In this post we will learn about Batch Apex.
Using Synchronous Apex we can’t perform below operations.
- SOQL on more than 50 thousand records.
- DML on more that 10 thousand records.
To overcome these governor limit, Salesforce has introduced Batch Apex. Batch Apex is used to perform operation on thousands or millions of records that would exceed normal processing limits. Using Batch Apex, you can process records asynchronously in batches to stay within platform limits.
Each time you invoke a batch class, the job is placed on the Apex job queue and is executed as a discrete transaction.
Benefits
- Every transaction starts with a new set of governor limits, making it easier to ensure that your code stays within the governor execution limits.
- If one batch fails to process successfully, all other successful batch transactions aren’t rolled back.
Writing Batch Class
- To write a Batch Apex class , you have to write a global apex class which implement the Database.Batchable interface and include the following three methods:
- Start method : Automatically called at the beginning of the apex job. This method will collect record or objects on which the operation should be performed. These records are divided into subtasks & pass those to execute method.
- Execute Method: Performs an operation which we want to perform on the records fetched from start method.
- Finish method : Executed after all batches are processed. Use this method to send confirmation email notifications.
global class BatchClass implements Database.Batchable<sObject> {
global Database.QueryLocator start(Database.BatchableContext BC) {
String query = 'SELECT Id, Name, BillingCity FROM Account';
return Database.getQueryLocator(query);
}
global void execute(Database.BatchableContext BC, List<Account> accList){
for(Account acc : accList){
acc.Name = acc.Name + ' - '+ acc.BillingCity;
}
try {
update accList;
} catch(Exception e) {
System.debug(e);
}
}
global void finish(Database.BatchableContext BC) {
}
}
Invoking a Batch Class
To invoke a batch class, simply instantiate it and then call Database.executeBatch with the instance:
BatchClass batchObject = new BatchClass();
Id batchId = Database.executeBatch(batchObject);
You can also optionally pass a second scope parameter to specify the number of records that should be passed into the execute method for each batch.
Id batchId = Database.executeBatch(batchObject, 100);
Using State in Batch Apex
- To maintain state across transactions, you can specify Database.Stateful in the class definition.
- Only instance member variables retain their values between transactions. Static member variables don’t retain their values and are reset between transactions.
Using Callouts in Batch Apex
- To use a callout in batch Apex, specify Database.AllowsCallouts in the class definition.
Notes
- If you want to run batch on records that can be filtered by SOQL then QueryLocator is preferable, but if records that you want to be processed by batch can not be filtered by SOQL then you will have to use iteratable.
- Another notable difference is the QueryLocator can return millions of rows for a batch, whereas the Iterable can only pull in the synchronous query limit.
- The default batch size is 200 record.