Understanding SalesForce Apex Batch Class

Salesforce Apex batch is an important concept of batch command processing for complex and long-running processes in an asynchronous manner. The major benefit that we get from the Apex batch class is that we can overcome the SalesForce limitation of SOQL queries (total number of SOQL queries governor limit for synchronous operations being 100 and asynchronous operations being 200) that are executed in a single run. We can perform the manipulation of records in batches.

Implementation Process:

But for understanding the implementation of processing the SOQL operation in batch mode, I am simply taking the set of 22 records and to perform the update query to update 8 records in one batch run, in other words, there will be total 3 batches with first two batches updating the 8 records and third batch will update the 6 left records.

Suppose I have a Contact object with custom fields “Level__c” and “Communication_prefrerence__c”.
Level__c is a picklist field with values “Top”, “Intermediate” and “bottom” position in the organization, Communication_prefrerence__c is a picklist field with values “Email”, “Mail” and “Call”.

I want to update the field “Communication_prefrerence__c” in the Contact Object based on the Level to which Contact belongs.

Batch Apex Class Code for this update operation is as follows:

global class ContactUpdateBatchClass implements Database.Batchable {
global Iterable start(Database.BatchableContext contactbc) {
System.Debug('Start-------------------------------');
List contactListObj=[Select Id,Name,Level__c,Communication_Preference__c from Contact Limit 22];
return contactListObj;
}
global void execute(Database.BatchableContext contactbc, List contactscope) {
System.Debug('Execute-------------------------------');
System.Debug('Arguement Size :'+contactscope.Size());
For (Contact contact :contactscope) {
If (contact.Level__c == 'Top') {
contact.Communication_Preference__c='Email';
}
else if (contact.Level__c == 'Intermediate' ) {
contact.Communication_Preference__c='Call';
}
else
contact.Communication_Preference__c='Mail';
}
update contactscope;
}
global void finish(Database.BatchableContext contactbc) {
System.Debug('Finish.........................................................');
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
String[] toAddresses = new String[] {'infol@webners.com'};
mail.setToAddresses(toAddresses);
mail.setSubject('Apex Batch Job Status');
mail.setPlainTextBody('The batch Apex job has been finished');
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
}
}

In the Developer Console Anonymous Window, write the following query:

Id ContactUpdate_batchJob_id= Database.executeBatch(new ContactUpdateBatchClass(),8);
System.debug('Job id ------: '+ContactUpdate_batchJob_id);

Here executeBatch method takes the arguments as the new instance of your batch class and in the second argument, we are specifying the batch size as 8, i.e no. of records to be processed. This argument is optional and by default, its value is 200.

You will be able to track the status of the Apex in the Developer Console, where you will find 6 logs for this batch class run.
3 logs will be for the execute method for each batch run.
1 log for the Start action in the batch.
1 log for the Finish action in the batch.
1 log for the Developer console Anonymous Window from where we are running the batch class query.