All you need to Know about Salesforce Batch Apex

Salesforce batch apex

One of the powerful features of Salesforce is Batch Apex, which allows developers to process large amounts of data in a single batch. In this article, we will delve deeper into what Batch Apex is, how it works, and its benefits.

What is Salesforce Batch Apex?

Batch Apex is a Salesforce feature that allows developers to process large amounts of data in an asynchronous manner. It enables developers to divide a large job into multiple smaller jobs, or batches, which are processed separately. This is particularly useful when dealing with large data sets that cannot be processed in a single transaction.

How does Salesforce Batch Apex work?

Batch Apex works by processing data in batches of up to 2000 records at a time. Developers can specify the size of each batch and the number of batches to be processed.Salesforce automatically creates a new batch when the previous batch has completed processing.

  • Default Batch Size : 200
  • Minimum Batch Size : 1
  • Maximimum Batch Size : 2000

When a Batch Apex job is started, it is added to a queue and is processed asynchronously by the Salesforce platform. This means that the job runs in the background, allowing the user to continue working on other tasks.

What are the benefits of Salesforce Batch Apex? over the Other asynchronous methods ?

In comparison to other asynchronous methods in Salesforce, Batch Apex has the following advantages:

  1. Batch Apex can process up to 50 million records at a time, which is significantly higher than other asynchronous methods.
  2. Batch Apex allows developers to specify the size of each batch and the number of batches to be processed, which gives them greater control over the process.
  3. Batch Apex has built-in error handling, which ensures that data is processed correctly. If a batch fails to complete, the system automatically retries the batch, reducing the need for manual intervention.
  4. Batch Apex is ideal for long-running jobs that require periodic monitoring or that need to be run on a schedule.
  5. Batch Apex provides more efficient and flexible processing of large amounts of data in Salesforce.

How does Batch Apex work?

Batch Apex works by breaking down large volumes of data into smaller batches, which are processed in separate transactions. A batch job is created by implementing the Salesforce-provided interface, ‘Database.Batchable’. This interface includes three methods that must be implemented:

1. start(): This method is called at the beginning of the batch job and is used to return a query locator that identifies the records to be processed.

2. execute(): This method is called for each batch of records and is used to process the data. It takes a list of sObjects as input and returns void.

3. finish(): This method is called at the end of the batch job and is used to perform any final cleanup or post-processing.

Once the batch job is created, it can be executed using the ‘Database.executeBatch’ method. This method takes the batch job as input and executes it in the background, processing the data in separate transactions.

Apex Batch Job Script

global class BatchName 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<sObject> scope) {
        // Your batch processing logic here
        for (sObject record : scope) {
            // Do something with each record
        }
    }
    global void finish(Database.BatchableContext bc) {
        // Your finish logic here
    }
}

This is just a simple example, but you can customize the start, execute, and finish methods to fit the specific needs of your Batch Apex job.

Best Practices for using Batch Apex

  • Use query locators: When processing large volumes of data, it is best to use query locators to retrieve the records to be processed. This ensures that only the required data is retrieved, which can help to reduce processing time.
  • Optimize execute() method: The execute() method is where the data is processed, so it is essential to optimize this method for efficiency. Consider using bulkification techniques, such as using collections and limiting the number of DML statements.
  • Monitor governor limits: Batch Apex is subject to Salesforce governor limits, so it is important to monitor these limits and ensure that they are not exceeded.

QueryLocator VS Iterable

  • QueryLocator is the default option for Batch Apex and is recommended for most use cases. It is a Salesforce-specific interface that allows you to execute a SOQL query to retrieve the records to be processed. QueryLocator is optimized for large data sets and allows you to efficiently retrieve and process records in batches. Additionally, QueryLocator supports query cursors, which enable you to process records in a consistent and predictable order.
  • Iterable allows you to process records from any data source that implements the Iterable interface. This can include objects, lists, or even custom data structures. Iterable is a more flexible option compared to QueryLocator and allows you to customize the logic for retrieving the records to be processed. However, Iterable is not optimized for large data sets and can potentially hit the governor limits if not implemented carefully.
  • When deciding between QueryLocator and Iterable, you should consider the size of the data set and the complexity of the processing logic. QueryLocator is recommended for large data sets and complex processing requirements, while Iterable is better suited for smaller data sets and simpler processing logic. Additionally, you should consider the source of the data and whether it can be efficiently queried using SOQL.

example of using QueryLocator and Iterable in Apex code:
Using QueryLocator:

global class MyBatchJob implements Database.Batchable<sObject> {

global Database.QueryLocator start(Database.BatchableContext BC) {
// Use a query locator to retrieve records in batches
return Database.getQueryLocator([SELECT Id, Name, Status FROM MyCustomObject__c WHERE Status = 'Open' ORDER BY Name]);
}

global void execute(Database.BatchableContext BC, List<MyCustomObject__c> scope) {
// Process the records in the current batch
for(MyCustomObject__c obj : scope) {
obj.Status = 'Processed';
// Perform other processing logic as needed
}
update scope;
}

global void finish(Database.BatchableContext BC) {
// Perform any post-processing tasks as needed
}
}
  • Using Iterable :
global class MyBatchJob implements Database.Batchable<sObject> {

global Iterable<sObject> start(Database.BatchableContext BC) {
// Use a list to retrieve the records to be processed
List<sObject> records = [SELECT Id, Name, Status FROM sObject];
return records;
}

global void execute(Database.BatchableContext BC, List<sObject> scope) {
// Process the records in the current batch
for(sObject obj : scope) {
obj.Status = 'Processed';
// Perform other processing logic as needed
}
update scope;
}

global void finish(Database.BatchableContext BC) {
// Perform any post-processing tasks as needed
}
}

In the first example, we use a QueryLocator in the start() method to retrieve the records in batches. The execute() method processes each batch of records and updates their Status field as needed. Finally, the finish() method can perform any necessary post-processing tasks.
In the second example, we use an Iterable to retrieve the records in the start() method. The execute() and finish() methods are the same as in the QueryLocator example, but the difference is that the query is executed immediately and all of the records are loaded into memory. This can be problematic if the data set is very large, as it can lead to memory issues and performance problems.

Use cases of Batch Apex in real time

Top 10 real-time scenarios where Batch Apex can be used in Salesforce:

  1. Mass data updates: Batch Apex can be used to update large volumes of data, such as updating the status of all open opportunities at the end of a quarter.
  2. Lead conversion: Batch Apex can be used to convert a large number of leads into contacts, accounts, and opportunities, while preserving data integrity.
  3. Data integration: Batch Apex can be used to integrate data from external systems into Salesforce, such as synchronizing customer data from a third-party CRM.
  4. Archiving old data: Batch Apex can be used to archive old records that are no longer needed, such as closed cases or expired contracts.
  5. Data cleansing: Batch Apex can be used to clean up and standardize data, such as removing duplicate records or updating field values based on certain criteria.
  6. Email notifications: Batch Apex can be used to send email notifications to a large number of users, such as notifying all users of a system downtime or scheduled maintenance.
  7. Lead Assignment or Task Assignment : Batch Apex can be used assign leads or tasks to users or queues in Salesforce.
  8. Record deletion: Batch Apex can be used to delete a large number of records that meet certain criteria, such as deleting all records of a custom object that is no longer needed.
  9. Complex calculations: Batch Apex can be used to perform complex calculations on large volumes of data, such as calculating a customer’s lifetime value based on their purchase history.
  10. Data backups: Batch Apex can be used to back up data from Salesforce to an external system, such as creating daily backups of critical data to ensure business continuity.

Leave a Reply

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