Batch Apex In Salesforce
#kb_sfdc_content
Content
Batch Apex
Key Components of Batch Apex
Batch Job Execution
Using Callouts in Batch Apex
Schedular Class for Batch Apex
Test Class for Batch Job
Best Practices for Batch Apex
#kb_sfdc_content
Batch Apex
To use batch Apex, write an Apex class that implements the Salesforce-provided interface
Database.Batchable and then invoke the class programmatically.
To monitor or stop the execution of the batch Apex job,
from Setup, enter Apex Jobs in the Quick Find box, then select Apex Jobs.
Batch Apex allows you to process large sets of records asynchronously in Salesforce.
It is particularly useful when you need to work with a large volume of data that cannot be processed
synchronously due to governor limits.
#kb_sfdc_content
Key Components of Batch Apex:
1. Start Method:
- The start method is responsible for returning a database query locator that defines the scope of records to be
processed.
- It runs once at the beginning of the batch job and returns a set of records or IDs to be processed.
2. Execute Method:
- The execute method processes the records returned by the start method.
- It runs multiple times, once for each batch of records specified by the scope size and until all records have been
processed.
3. Finish Method:
- The finish method runs once after all batches have been processed.
- It can be used for any post-processing tasks or cleanup operations.
Execution –
#kb_sfdc_content
Using Callouts in Batch Apex
To do the callout in batch Apex, use Database.AllowsCallouts in the class definition. Like below
example.
public class SearchAndReplace implements Database.Batchable<sObject>,
Database.AllowsCallouts
{
#kb_sfdc_content
Scheduler Class For Batch Apex
Schedulable Class is a global class that implements the Schedulable interface.
That includes one execute method. Here is example of a scheduler class
Example
global class AccountUpdateBatchJobscheduled implements Schedulable {
global void execute(SchedulableContext sc) {
AccountUpdateBatchJob b = new AccountUpdateBatchJob();
database.executebatch(b);
}
}
#kb_sfdc_content
Test Class for Batch Job
@isTest
public class AccountUpdateBatchJobTest {
static testMethod void testMethod1() {
List<Account> lstAccount= new List<Account>();
for(Integer i=0 ;i <200;i++) {
Account acc = new Account();
acc.Name ='Name'+i;
lstLead.add(acc);
}
insert lstAccount;
Test.startTest();
AccountUpdateBatchJob obj = new AccountUpdateBatchJob();
DataBase.executeBatch(obj);
Test.stopTest();
}
}
#kb_sfdc_content
Best Practices for batch Apex
As with future methods, there are a few things you want to keep in mind when using Batch Apex. To
ensure fast execution of batch jobs, minimize Web service callout times and tune queries used in your
batch Apex code. The longer the batch job executes, the more likely other queued jobs are delayed when
many jobs are in the queue. Best practices include:
• Only use Batch Apex if you have more than one batch of records. If you don’t have enough records to
run more than one batch, you are probably better off using Queueable Apex.
• Tune any SOQL query to gather the records to execute as quickly as possible.
• Minimize the number of asynchronous requests created to minimize the chance of delays.
• Use extreme care if you are planning to invoke a batch job from a trigger. You must be able to guarantee
that the trigger won’t add more batch jobs than the limit.
#kb_sfdc_content
#kb_sfdc_content