What is Batch Processing In a Salesforce and Why do we Use it Explain with Examples
- Abhilash Banpurkar
- Feb 11, 2023
- 3 min read
Salesforce is a powerful platform that allows businesses to manage their customer relationships, sales, and other important processes. However, when it comes to processing large amounts of data, there are limits that need to be considered to ensure the stability and performance of the platform. That's where Batch Apex comes in.
Batch Apex is a feature in Salesforce that allows you to run large jobs (up to 50 million records) by dividing the job into smaller, manageable chunks called "batches". This makes it possible to process large amounts of data that would normally exceed governor limits, which are limits imposed by Salesforce to ensure the stability and performance of the platform.
With Batch Apex, you can perform complex operations on large amounts of data in a way that is both efficient and scalable, while still adhering to the governor limits of the Salesforce platform.
Here's an example of how you might use Batch Apex in Salesforce:
Create a custom Apex class that implements the Database.Batchable interface and specifies the batch size and query to be used.
In the execute method, perform the desired operation (such as updating a field) on each batch of records.
Submit the batch job using the Apex Scheduler, the System.scheduleBatch method, or by calling the class in Anonymous Apex.
Monitor the progress of the batch job using the Apex Jobs page in the Salesforce user interface.
Methods in a Batch Apex-
1. Start - fetch the records using SOQL
2. Execute - Process records or write logic over records
3. Finish - past processing logic.
Can remain empty. - Using SOQL, status of the batch job can be monitored by querying the AsyncApexJob table.
The same is also possible from “Apex Jobs”in setup.
Higher limits in Batch Context-
200 SOQL per transaction (instead of 100)
Records retrieved by SOQL 50 Million (instead of 50K)
Executed Code statements 12,00,000(instead 6,00,000) - Heap size 12 MB (instead 6 MB)
Syntax:
Public class ExampleBatchJob implements Database.Batchable {
public Database.QueryLocator start(Database.BatchableContext BC){
//Use soql to retrieve data //
return Database.getQueryLocator(query);
}
public void execute(Database.BatchableContext BC, List scope){
//Here scope == returned value of start method
}
public void finish(Database.BatchableContext BC){
//Post processing logic // Optional - can be empty
}
Once the batch job is complete, you can check the results to see if it was successful or if any errors occurred. You can submit the batch job using the Apex Scheduler, the System.scheduleBatch method, or by calling the class in Anonymous Apex. Additionally, you can monitor the progress of the batch job using the Apex Jobs page in the Salesforce user interface.
Calling or Execution Part in Ananymous Windows:
ExampleBatchJob batchApex = new ExampleBatchJob ();
DataBase.executeBatch(batchApex);
// Returns jobId of AsyncApexJob
OR
DataBase.executeBatch(batchApex, 100 );
//Here maximum value= 2000
//if no size specified then it takes 200 per batch.
Example -Update Account by batch :
public class ExampleBatchApex implements Database.Batchable {
public Database.QueryLocator start(Database.BatchableContext BC){
String query = 'SELECT Id,Name FROM Account limit 10';
return Database.getQueryLocator(query);
}
public void execute(Database.BatchableContext BC, List scope){ for(Account a : scope){
a.Name = a.Name + 'Updated by Batch job';
}
update scope;
}
public void finish(Database.BatchableContext BC) { }
}
// Calling
ExampleBatchApex batchApex = new ExampleBatchApex(); System.debug('Batch Job Id: '+ DataBase.executeBatch(batchApex));
Benefits of using Batch Apex:
Batch Apex offers several benefits for businesses using Salesforce:
Increased efficiency: By dividing large amounts of data into smaller batches, Batch Apex reduces the time and resources required to process the data. This leads to increased efficiency and improved performance.
Scalability: Batch Apex makes it possible to process large amounts of data while still adhering to the governor limits of the Salesforce platform. This means that as your business grows and the amount of data you need to process increases, Batch Apex will continue to be a viable solution.
Improved accuracy: Batch Apex allows you to perform complex operations on large amounts of data in a way that is consistent and reliable. This leads to improved accuracy and reduced errors.
Conclusion:
Batch Apex is a valuable tool for businesses using Salesforce that need to process large amounts of data. It offers increased efficiency, scalability, and improved accuracy, making it possible to perform complex operations on large amounts of data in a way that is both efficient and scalable. If you're looking to maximize the performance of your Salesforce platform, Batch Apex is definitely worth considering.
Comments