Hello folks, Here we will see what is Batch Apex in Salesforce? Batch Apex allows you to execute a piece of apex code asynchronously. It is basically used for processing large data and provide greater flexibility over governer limit than the synchronous code.

After completing this tutorial you will be able to understand:

  • What is Batch Apex in Salesforce?
  • Why we need Batch Apex?
  • How does Batch Apex work?
  • How to use Batch Apex?

What you’ll do?

  • Create a simple Batch Apex class that will simply update Account Records.

What is Batch Apex in Salesforce?

Batch Apex allows you to execute a piece of apex code asynchronously. It is basically used for processing large data and provide greater flexibility over governer limit than the synchronous code.

Why we need Batch Apex?

Suppose I want to perform field update on all the record of Standard Account object in salesforce organization, then governor limits would restrict from field updating of every record. Because in a single transaction we can update only 10000 records. So if you have more than 10000 records to update you can use Batch apex.

How does Batch Apex work?

Batch apex does not perform an update operation on all the records instead it divides them into no of subtasks. Batch apex will fetch all the records that need to update and divide them into a batch of 200 records. The update operation is performed on every batch of 200 records separately.

How to use Batch Apex?

To implement Batch apex we need to implement Database.Batchable interface.

// Batch Job for Processing the Records
global class ExampleBatch implements Database.Batchable<sobject> {
 
// Start Method
global Database.Querylocator start (Database.BatchableContext BC) {

}
 
// Execute method
global void execute (Database.BatchableContext BC, List<sobject> scope) {
 
 
}
 
// Finish Method
global void finish(Database.BatchableContext BC) {
 
}
}

The Database.Batchable interface provides three methods:
  • Start.

  • Execute

  • Finish

Start:

This is the first method that will call automatically. This method collects the records or objects on which the operation should perform and divide them into subtask and transfer to execute method.

Execute:

This method performs the operation (for example updating records) on the records fetched from the start method.

Finish:

The finish method is called in the last when all batches are processed. This method can be used to send confirmation email notification.

Example:

Here I am creating a simple example of Batch Class that will simply update Account Name of Account Object.

global class batchAccountUpdate implements Database.Batchable<sobject> {
 
global Database.QueryLocator start(Database.BatchableContext bc){
 
String query = 'SELECT Id, Name FROM Account';
return Database.getQueryLocator(query);
}
 
global void execute(Database.BatchableContext bc, List<account> scope) {
 
for(Account a : scope) {
a.Name = a.Name + 'Updated';
}
update scope;
} 
 
global void finish(Database.BatchableContext bc){
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setToAddresses(new String[] {'youremal@domain.com'});
mail.setReplyTo('youremal@domain.com');
mail.setSenderDisplayName('Account Batch Processing');
mail.setSubject('Account Batch Process Completed');
mail.setPlainTextBody('Account Batch Process has completed');
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
}
}

Scheduling Batch Apex:

Schedule a Batch apex you need to implement a Schedulable interface which provides an execute method.

Global class batchAccountUpdateSchedular implements Schedulable{
Global void execute(SchedulableContext sc){
batchAccountUpdate b= new batchAccountUpdate();
Database.executebatch(b);
}
}

Now you can Schedule this apex class by following the below steps:

Step 1: Goto Setup>Apex Classes.

Batch Apex

Step 2: Now Click on Schedule Apex.

Batch Apex

Step 3: Prvoide all the details like name, class to schedule, start time and frequency and Save. Here we done.

View More:

Conclusion:

I hope you liked this post. Please don’t hesitate to comment for any technical help. Your feedback and comments are always appreciated.

Thank You.

Leave a Comment