All About Batch Class in Salesforce | The Developer Guide
Batch Apex is used to run large jobs (think thousands or millions of records!) that would exceed normal processing limits. Each time you invoke a batch class, the job is placed on the Apex job queue and is executed as a discrete transaction.
Type Of Batch Apex
Don't forget to check out: Deleting Apex Classes / Apex Triggers From Production Using Workbench | Salesforce Tutorial
Example of Batch Apex
Batch Class: Update All the Old Opportunities in Org using Batch with criteria :
Update custom picklist field “Type”
If Amount is >=100 then “LITE”,
Else if Amount is >=200 then “MEDIUM” ,
Else if Amount is >=300 then “ADVANCE”
Also, create a scheduler class to run the batch job at 01:00 AM.
Batch Class Code
global class LeadProcessor implements Database.Batchable<Sobject> ,Schedulable{
string Query;
global Database.QueryLocator start(Database.BatchableContext bc) {
Query = 'Select id, name, Amount,StageName,Custom_Type__c,Type From Opportunity';
return Database.getQueryLocator(query);
}
global void execute(Database.BatchableContext bc, List<Opportunity> Scope1){
for(Opportunity p : Scope1){
// system.debug('@'+p.stage__c);
if(p.Amount <= 100){
p.Custom_Type__c='LITE';
}
else if(p.Amount <= 200){
p.Custom_Type__c='MEDIUM';
}
else if (p.Amount <= 300){
p.Custom_Type__c='ADVANCE';
}
else{
p.Type='Prospect';
}
// Scope1.add(p);
}
update scope1;
}
global void finish(Database.BatchableContext bc){ }
global void execute(SchedulableContext sc) {
LeadProcessor b = new LeadProcessor();
Database.executeBatch(b,15);
}
}
Check out an amazing Salesforce video tutorial here: Skill Based Routing with Apex/Flows in Salesforce
Anonymous Window (Ctrl + E) Code
LeadProcessor b = new LeadProcessor();
String sch = '30 12 17 18 10 ?';
// In this String sch 30 represent a second
//In this String sch 12 represent a minutes
//In this String sch 17 represent a hours (5 PM).
//In this String sch 18 represent a day of the month
//In this String sch 10 represent a month of the year
//In this String sch ? represent a current Year
system.schedule('Job Name9', sch, b);

Responses