Introduction to Batch Apex Class in Salesforce

Salesforce, the world’s leading CRM provider, does not allow the execution of DML operations for more than 10,000 records in a single transaction. Due to the governor limits in Salesforce, you won’t be able to perform DML operation for more than 10k records. This is where Batch Apex processing comes into the picture.

What is Batch Apex Processing?

Batch Apex processing is the innovative feature provided by Salesforce where you can execute thousands and millions of records. It is particularly designed for processing a large number of records, which means the record is divided into small batches of records and each batch will be processed separately to stay within the governor limits.

Benefits of Batch Apex in Salesforce

  • For each transaction, Batch Apex ensures that the code stays within the governor limit.
  • Batch Apex won’t execute or even consider executing the other batches of records, until and unless the one batch is not successfully executed.
  • A comprehensive set of records can be executed together using Batch Apex classes.
  • The interface can be scheduled for batch execution at different periods.
  • Asynchronous operations can be implemented through Batch Apex classes.
  • Batch Jobs are invoked programmatically at run time (during the execution process) and can be operated on any size of records, with a maximum of 200 batch records. You can split larger record data into 200 records per batch for better performance.

What is Batchable Interface?

You need to implement a “Database. Batchable”, while using Batch Apex in Salesforce. This interface consists of three methods:

Start
Execute
Finish

Start – This method is automatically invoked at the starting of the apex job. This method collects records or objects on which the operation will be performed. These records are divided into batches and pass them to execute the method. In a few cases, the ‘QueryLocator’ method is used to operate with the simple SOQL query to define the scope of objects inside a Batch Job.

Syntax:
global void execute(Database.BatchableContext BC, list<sobject<) {}

Execute: This method operates after the Start method. This method performs the actual processing for each batch of records fetched from the start method, separately.

Syntax:
global void execute(Database.BatchableContext BC, list<sobject<) {}

Finish: This method executes at the end after all the batches have been successfully executed. This method is used to perform post-processing operations such as sending confirmation email notifications.

Syntax:
global void finish(Database.BatchableContext BC) {}

Governor Limits for Batch Apex

  • Before writing the batch apex code, you need to consider the governor’s execution limits to ensure the efficient use of classes.
  • It allows the maximum of five active or queued batch jobs for Apex.
  • One user can open up to 50 query cursors for a certain duration. If the user tries to open a new one, the oldest 50 cursors will be released.

Note: This limit is different for the Batch APEX Start Method that could have a maximum of five query cursors open at the same time.

Besides the start method, the other methods have limit up to 50 cursors. This limit is tracked differently for multiple features in Force.com. For instance, for a particular method, you could have 50 batch cursors, 50 Apex query cursors, and 50 VisualForce cursors open at a particular time together.

A maximum of 50 million records can be returned to the batch Apex database at a particular time. Once all the 50 million records are returned, the batch job will be terminated automatically and finally marked it as “Error”.

The default size for a record set in batch APEX is 200. The governor limits must be redefined for each set of records as per the requirement.

When talking about the callouts then start, execute, and the finish methods are limited to ten callouts per method execution.

You can execute a maximum number of 250,000 batch execution in 24 hours.

At a single time, only one start method will run for an organization. The batch jobs that are not started yet will stay in the queue and will be executed later.

Batch Class Example
global class removeDuplicateRecords implements Database.Batchable
{
global Database.QueryLocator start(Database.BatchableContext BC)
{
String query = ‘select Email,Name,Company,Description from Lead where Email != Null’;
return Database.getQueryLocator(query);
}

global void execute(Database.BatchableContext BC, List scope)
{
Map<String,Lead> EmailLead = new Map<String,Lead>();
List LeadInfo = new List();
List duplicatelist = new List();
for(Lead objLead : scope)
{
if(!EmailLead.containsKey(objLead.Email))
{
EmailLead.put(objLead.Email,objLead);
}
else
{
duplicatelist.add(objLead);
}
}
for(Lead objLead1 : duplicatelist)
{
Lead lead_new = EmailLead.get(objLead1.Email);
if(lead_new.Description != Null)
{
lead_new.Description = lead_new.Description + (‘;’+ objLead1.Name +’,’+ objLead1.Company);
LeadInfo.add(lead_new);
}
else
{
lead_new.Description = (objLead1.Name +’,’+ objLead1.Company);
LeadInfo.add(lead_new);
}
}
if(LeadInfo.size() > 0)
{
update LeadInfo;
}
if(duplicatelist.size() > 0)
{
delete duplicatelist;
}
}
global void finish(Database.BatchableContext BC)
{

}
}

Conclusion
Batch Apex is used to operate a high volume of records and it allows you to run the code at a certain duration. With Batch Apex, you can process records asynchronously in batches. It allows you to split records into multiple batches to execute them separately.

Author: AJ

Share This Post On

Submit a Comment

Your email address will not be published. Required fields are marked *

× How can I help you?