
What is Salesforce Batch Apex?

Table of Contents
- Understanding Batch Apex in Salesforce
- Why Batch Apex Is Important
- How Batch Apex Works
- Common Use Cases
- Example of Batch Apex
- Sample Batch Job
- What is stateful in the batch apex?
- How to execute a Batch job?
- Scheduler Class For Batch Apex
- How to Schedule scheduler class?
- Test Class for Batch Job
- Batch Apex Limits
Batch Apex in Salesforce is like a conveyor belt for processing large amounts of data efficiently. It’s a way to break down big tasks into smaller, manageable chunks, allowing you to work with massive datasets without overloading the system. Think of it as a conveyor belt in a factory, moving items in batches rather than all at once.
What is Batch Apex?
Batch Apex in Salesforce is a powerful tool designed for processing large volumes of data efficiently. It allows developers to execute complex operations on records in batches, overcoming the platform’s governor limits for SOQL queries and DML operations. A Batch Apex job is divided into three methods: start
, which collects the data to be processed; execute
, which processes each batch of records; and finish
, which handles any post-processing tasks. This asynchronous processing capability ensures that large datasets can be managed without performance degradation, making it ideal for tasks like data cleansing, archiving, or large-scale updates. Batch Apex enhances scalability and performance, ensuring robust and efficient data management in Salesforce applications.
Looking to ace Salesforce job interviews? These Utimate Salesforce Interview Questions will guide you to success!
Why Batch Apex Is Important?
Batch Apex is crucial in Salesforce for handling large data volumes efficiently. Salesforce’s governor limits restrict the number of SOQL queries and DML operations that can be executed in a single transaction. Batch Apex addresses this by processing data in smaller, manageable chunks, enabling developers to perform extensive data operations like bulk updates, deletions, and data migrations without hitting these limits. This capability is essential for maintaining data integrity and performance in applications that manage large datasets.
CRS Info Solutions provides a top-tier Salesforce course for beginners, offering expert instruction and a solid foundation to help you achieve certification and excel in interviews.
Another significant advantage of Batch Apex is its asynchronous processing nature. Unlike synchronous processing, which can impact system performance and user experience due to long-running operations, Batch Apex runs jobs in the background. This ensures that complex computations and data processing tasks do not interfere with regular system operations, providing a seamless user experience while performing necessary backend tasks.
Batch Apex also enhances scalability and efficiency in Salesforce implementations. By allowing developers to perform bulk operations efficiently, it ensures optimal resource utilization and robust performance. This makes Batch Apex a vital tool for large-scale Salesforce applications, where managing and processing large volumes of data are critical for operational success.
How Batch Apex Works?
Batch Apex in Salesforce is designed to handle large data processing tasks efficiently by breaking them down into manageable chunks. Here’s a step-by-step overview of how it works:
1. Defining the Batch Class
To start using Batch Apex, you need to define a batch class that implements the Database.Batchable
interface. This interface requires three methods: start
, execute
, and finish
.
global class SampleBatchClass implements Database.Batchable<sObject> {
global Database.QueryLocator start(Database.BatchableContext BC) {
// Code to collect the records to be processed
}
global void execute(Database.BatchableContext BC, List<sObject> scope) {
// Code to process each batch of records
}
global void finish(Database.BatchableContext BC) {
// Code to execute after all batches are processed
}
}
2. Collecting the Data: start
Method
The start
method is used to collect the data that needs to be processed. This method typically returns a Database.QueryLocator
object or an iterable list of records. The query defined here determines the scope of records that will be processed in batches.
global Database.QueryLocator start(Database.BatchableContext BC) {
return Database.getQueryLocator('SELECT Id, Name FROM Account');
}
3. Processing Each Batch: execute
Method
The execute
method processes each batch of records. Salesforce divides the total number of records into smaller batches, which are passed to this method as a list. This method contains the logic for processing each chunk of records, ensuring that operations are performed within governor limits.
global void execute(Database.BatchableContext BC, List<Account> scope) {
for (Account acc : scope) {
// Processing logic here
}
update scope;
}
4. Finalizing the Job: finish
Method
The finish
method is called after all batches have been processed. This method can be used to perform any final operations, such as sending notifications or executing follow-up processes.
global void finish(Database.BatchableContext BC) {
// Finalization logic, such as sending a notification
}
5. Running the Batch Job
To execute the batch job, you instantiate the batch class and call the Database.executeBatch
method, specifying the batch size. Salesforce then handles the execution, processing each batch asynchronously.
SampleBatchClass batch = new SampleBatchClass();
Database.executeBatch(batch, 200);
By following these steps, Batch Apex allows you to process large datasets efficiently, ensuring that complex operations are handled within Salesforce’s governor limits and maintaining system performance.
Common Use Cases
5 Common Use Cases for Batch Apex in Salesforce
Batch Apex is an invaluable tool in Salesforce for handling large data volumes and complex processing tasks. Here are five common use cases, explained in detail:
1. Data Cleansing and Deduplication
Use Case: Over time, Salesforce data can become cluttered with duplicate or outdated records, which can affect data integrity and reporting accuracy.
Batch Apex can be used to systematically clean and deduplicate records. For instance, you might run a batch job that identifies and merges duplicate records based on specific criteria, such as matching email addresses or phone numbers. The start
method would query all relevant records, the execute
method would process each batch to identify duplicates, and the finish
method could log the results or send a summary email.
2. Mass Data Updates
Use Case: Business requirements often necessitate updating large numbers of records simultaneously, such as during a policy change or data migration.
Batch Apex is ideal for performing mass updates. For example, if a company changes its sales process, a batch job could update all existing opportunity records to reflect new stages or criteria. The start
method would retrieve the records to be updated, the execute
method would apply the necessary changes to each batch, and the finish
method might trigger a report generation or notify stakeholders of the completion.
3. Scheduled Data Maintenance
Use Case: Regular maintenance tasks, such as recalculating metrics or resetting fields, are necessary to keep Salesforce data accurate and up-to-date.
Batch Apex can automate these routine tasks. For example, a batch job could recalculate and update custom fields that track monthly sales metrics. Scheduled to run at the end of each month, the start
method would select the records, the execute
method would perform the calculations and updates, and the finish
method could log the operation’s completion or send notifications.
4. Data Archival
Use Case: Archiving old data helps in managing database size and performance by moving or deleting records that are no longer actively used.
A Batch Apex job can identify and archive records based on predefined criteria, such as opportunities that have been closed for more than a year. The start
method would query these records, the execute
method would either delete or move them to an archival object, and the finish
method could ensure that all necessary records have been processed and confirm the archival task’s completion.
5. Complex Calculations
Use Case: Performing complex calculations across a large dataset, such as computing annual bonuses based on sales performance, requires substantial processing power.
Batch Apex can handle these intensive calculations efficiently. For example, a batch job could calculate annual bonuses for each salesperson based on their yearly sales performance. The start
method would retrieve all relevant sales data, the execute
method would calculate the bonuses for each batch of records, and the finish
method might update a separate bonus records object and notify the payroll department of the new figures.
By utilizing Batch Apex for these use cases, Salesforce administrators and developers can ensure efficient, reliable, and scalable processing of large datasets, maintaining the performance and integrity of their Salesforce environments.
CRS Info Solutions offers real-time Salesforce course for beginners designed to equip learners with practical knowledge and industry skills in Salesforce. Enroll for demo today.
Example of Batch Apex
Here’s a simplified example of how Batch Apex works:
Let’s say you have 10,000 customer records to update. Instead of updating all of them at once, you create a Batch Apex job that processes 1,000 records at a time. The job runs in the background, processing each batch sequentially until all records are updated. This approach ensures that the system doesn’t get overwhelmed and that you stay within the system’s resource limits.
public class MyBatchApex implements Database.Batchable<SObject> {
public Database.QueryLocator start(Database.BatchableContext context) {
return Database.getQueryLocator('SELECT Id, Name FROM Account');
}
public void execute(Database.BatchableContext context, List<Account> scope) {
// Process each batch of accounts here
}
public void finish(Database.BatchableContext context) {
// Finalize the job
}
}
In this code, MyBatchApex
is a Batch Apex class that processes batches of Account records.
Sample Batch Job
Below is a sample Batch Apex code example in Salesforce. This sample demonstrates how to create a simple Batch Apex class that updates the description field of Account records. It selects accounts where the description is null and updates them with a default description.
global class UpdateAccountBatch implements Database.Batchable<sObject> {
// Query to fetch Account records with null descriptions
global String query = 'SELECT Id, Name, Description FROM Account WHERE Description = NULL';
// This method is called at the start of the batch job to collect the records to process
global Database.QueryLocator start(Database.BatchableContext bc) {
return Database.getQueryLocator(query);
}
// This method is called for each chunk of records processed
global void execute(Database.BatchableContext bc, List<Account> records) {
// List to hold the accounts to be updated
List<Account> updatedAccounts = new List<Account>();
// Iterate over each record and set a default description
for(Account acc : records) {
acc.Description = 'Default description for accounts with no prior description.';
updatedAccounts.add(acc);
}
// Perform the update DML operation
update updatedAccounts;
}
// This method is called once after all chunks are processed
global void finish(Database.BatchableContext bc) {
System.debug('Batch job completed.');
}
}
Batch Apex is a valuable tool for handling large-scale data operations in Salesforce. It ensures that you can work with extensive datasets efficiently while adhering to Salesforce’s governor limits. As you become more familiar with Batch Apex, you’ll find it to be a crucial tool for managing and processing data in your Salesforce applications.
What is stateful in the batch apex?
In Batch Apex, the Database.Stateful
interface is used when you want to maintain state across the different batches of execution. Normally, every execute method of a Batch Apex class runs as a separate transaction, and all class variables are re-initialized between these transactions. However, by implementing the Database.Stateful
interface, you ensure that the values of instance variables are preserved between these separate transactions.
Why Use Database.Stateful
?
You might use Database.Stateful
if you need to count records processed, accumulate totals, or store data during the batch process that needs to be referenced or updated with each subsequent batch.
Example of Database.Stateful
Here’s an example where we use Database.Stateful
to count the number of accounts processed by the batch job. This counter is updated in each batch and preserved across the entire execution of the batch job.
global class CountAccountsBatch implements Database.Batchable<sObject>, Database.Stateful {
// This instance variable will maintain state across batches
global Integer count = 0;
// Query to fetch all accounts
global String query = 'SELECT Id, Name FROM Account';
// Start method
global Database.QueryLocator start(Database.BatchableContext bc) {
return Database.getQueryLocator(query);
}
// Execute method
global void execute(Database.BatchableContext bc, List<sObject> scope) {
// Increment count by the number of records processed in this batch
count += scope.size();
}
// Finish method
global void finish(Database.BatchableContext bc) {
System.debug('Total accounts processed: ' + count);
// Here you can also perform any cleanup or final operations like sending an email
}
}
Running the Batch Class
To run this batch class and see how count
preserves its value across different execute invocations, you can run it similarly to the previous example:
CountAccountsBatch batch = new CountAccountsBatch();
Database.executeBatch(batch);
This example clearly demonstrates how to use Database.Stateful
to keep track of data across different chunks of your batch process, which is particularly useful for accumulating results or managing complex state-dependent logic across batches.
How to execute a Batch job?
To execute a Batch Apex job in Salesforce, you use the Database.executeBatch
method. This method is typically called from an Apex script—like in the Salesforce Developer Console’s Execute Anonymous window—or from a custom Apex class that initiates the batch job based on some events or conditions.
Basic Syntax
The basic syntax to execute a Batch Apex class is:
Database.executeBatch(batchObject, batchSize);
batchObject
: An instance of your Batch Apex class.batchSize
: (Optional) The number of records to be passed into each execution of theexecute
method of your batch class. If you do not specify this parameter, Salesforce uses the default batch size of 200 records.
Example
Suppose you have a Batch Apex class named UpdateAccountBatch
. Here is how you would execute this batch job:
// Create an instance of the batch class
UpdateAccountBatch batch = new UpdateAccountBatch();
// Execute the batch job with the default batch size
Database.executeBatch(batch);
// Optionally, execute the batch job with a custom batch size, e.g., 100 records per batch
Database.executeBatch(batch, 100);
Points to Consider
- Governor Limits: The maximum batch size is 2,000 records. However, lower batch sizes might be more appropriate depending on the complexity of the operations in the
execute
method. - Testing and Deployment: Always thoroughly test your batch jobs in a sandbox or developer environment before deploying them to production. Consider all possible scenarios, including large data volumes.
- Monitoring and Logging: After initiating a batch job, you can monitor its progress in the Salesforce UI under Setup → Jobs → Apex Jobs. It’s also a good practice to implement robust logging, especially in the
finish
method, to capture the job outcome and any critical issues. - Error Handling: Implement try-catch blocks within your
execute
method to handle any exceptions gracefully. Also, consider what should happen if a batch job fails midway. Salesforce automatically tries to rerun the batches that have failed.
Using Database.executeBatch
effectively allows you to manage large data operations in Salesforce, ensuring they are broken into manageable pieces that comply with the platform’s governor limits.
Scheduler Class For Batch Apex
To schedule a Batch Apex class to run at specific intervals or times, you can use the Salesforce Apex Scheduler. This involves creating a separate Apex class that implements the Schedulable
interface. This scheduler class is responsible for initiating the Batch Apex job according to a specified Cron expression.
Step 1: Create the Batch Apex Class
Here is a basic Batch Apex class:
global class UpdateContactBatch implements Database.Batchable<sObject> {
global Database.QueryLocator start(Database.BatchableContext BC) {
return Database.getQueryLocator('SELECT Id, LastName FROM Contact WHERE LastName = NULL');
}
global void execute(Database.BatchableContext BC, List<Contact> scope) {
for (Contact contact : scope) {
contact.LastName = 'Updated LastName';
}
update scope;
}
global void finish(Database.BatchableContext BC) {
System.debug('Batch Process Finished');
}
}
Step 2: Create the Scheduler Class
The scheduler class will schedule the Batch Apex job. Here’s how you create a scheduler class for the batch class defined above:
global class ScheduleUpdateContactBatch implements Schedulable {
global void execute(SchedulableContext SC) {
UpdateContactBatch batch = new UpdateContactBatch();
Database.executeBatch(batch, 200); // Executing the batch job with a batch size of 200
}
}
Step 3: Schedule the Job in Salesforce
To schedule the job, you can either use the Salesforce user interface or execute a script in the Developer Console. Here’s how you would schedule it using the Developer Console with an Apex script:
// Scheduling the batch job to run every day at 1 AM
String cronExpr = '0 0 1 * * ?';
System.schedule('Update Contacts Daily', cronExpr, new ScheduleUpdateContactBatch());
Cron Expression
The Cron expression used here ('0 0 1 * * ?'
) specifies that the job should run daily at 1 AM. Here’s a breakdown of the expression:
0
: Seconds0
: Minutes1
: Hour*
: Day of month*
: Month?
: Day of week
Important Notes
- Testing: Always test your scheduler and batch classes in a sandbox or development environment before deploying them to production.
- Monitoring: After scheduling, you can monitor the scheduled jobs in Salesforce under Setup → Jobs → Scheduled Jobs.
- Limits: Be aware of governor limits for scheduling jobs and for batch jobs.
This setup allows your batch operations to be automated, running at regular intervals without manual intervention. This is especially useful for regular data maintenance tasks in Salesforce.
How to Schedule scheduler class?
Scheduling a Scheduler class in Salesforce can be accomplished in two main ways: through the Salesforce System Scheduler (via the Salesforce UI) or using the Developer Console. Both methods are effective for automating processes such as running batch jobs at regular intervals. Here’s how you can use each method:
1) Scheduling Through the System Scheduler
The System Scheduler allows you to schedule classes directly from the Salesforce Setup UI. This method is user-friendly and does not require writing any code.
Steps to Schedule Using the System Scheduler:
- Navigate to Setup: Log in to your Salesforce org and go to Setup.
- Open Scheduled Jobs: In the Quick Find box, type “Scheduled Jobs” and select it under the “Jobs” section.
- Schedule a New Job: Click on the “Schedule Apex” button. You will be taken to a new page to set up your scheduled class.
- Select the Apex Class: Choose the Apex class you want to schedule from the drop-down list. This class must implement the
Schedulable
interface. - Set the Schedule: Define the frequency of the job by setting the start date, end date, and preferred time, along with specifying the frequency (e.g., Daily, Weekly, or Monthly).
- Save the Job: After configuring the settings, save the job to activate the scheduling.
This interface provides a straightforward way to manage and review scheduled jobs without needing to write or execute scripts.
2) Scheduling via Developer Console
For those who prefer coding or need to schedule jobs programmatically, the Developer Console is the perfect tool. This involves writing a simple script using Apex to schedule your class.
Steps to Schedule Using the Developer Console:
- Open the Developer Console: From your Salesforce org, go to the Developer Console found under the “Developer Console” link in the dropdown menu from your username.
- Open Execute Anonymous Window: In the Developer Console, go to the “Debug” menu and select “Open Execute Anonymous Window.”
- Write the Scheduling Script: In the window, you can write a script using the
System.schedule
method. - Replace
MyScheduledClass
with the name of your class that implementsSchedulable
. - Execute the Script: Press the “Execute” button to schedule the job.
Here’s an example script:
String cron = '0 0 22 * * ?'; // Runs every day at 10 PM
String jobName = 'Nightly Data Cleanup';
MyScheduledClass sched = new MyScheduledClass();
System.schedule(jobName, cron, sched);
Test Class for Batch Job
Creating a test class for a Batch Apex job is essential to ensure that your code is robust and functions as expected under different conditions. In Salesforce, a test class helps verify the behavior of your batch class, and it’s also a requirement for deploying your code to a production environment, as Salesforce mandates a certain level of code coverage.
Example Batch Apex Job
Before we write the test class, let’s assume we have the following simple Batch Apex class that updates a field in Account records:
global class UpdateAccountDescriptionBatch implements Database.Batchable<sObject> {
global Database.QueryLocator start(Database.BatchableContext bc) {
return Database.getQueryLocator('SELECT Id, Description FROM Account WHERE Description = NULL');
}
global void execute(Database.BatchableContext bc, List<Account> accounts) {
for(Account acc : accounts) {
acc.Description = 'Updated Description';
}
update accounts;
}
global void finish(Database.BatchableContext bc) {
// Post-processing operations, if any
}
}
Test Class for the Batch Job
Now, let’s write a test class for this batch job. We’ll create some test data (accounts with null descriptions), execute the batch job, and assert that the descriptions are updated correctly.
@isTest
public class TestUpdateAccountDescriptionBatch {
static testMethod void testBatchUpdate() {
// Create test data
List<Account> testAccounts = new List<Account>();
for (int i = 0; i < 5; i++) {
Account acc = new Account(Name = 'Test Account ' + i);
testAccounts.add(acc);
}
insert testAccounts;
// Verify initial state
for (Account acc : [SELECT Description FROM Account WHERE Id IN :testAccounts]) {
System.assert(acc.Description == null, 'Initial Description should be null');
}
// Execute batch job
Test.startTest();
UpdateAccountDescriptionBatch batch = new UpdateAccountDescriptionBatch();
Id batchJobId = Database.executeBatch(batch);
Test.stopTest();
// Verify results
for (Account acc : [SELECT Description FROM Account WHERE Id IN :testAccounts]) {
System.assertEquals('Updated Description', acc.Description, 'Description should be updated');
}
}
}
Key Points in the Test Class
- Test Data Creation: The test creates a few Account records. Always ensure your test data is isolated and does not depend on data in the production or sandbox environment.
- Initial State Verification: Before running the batch, it checks that the descriptions are initially
null
, ensuring the batch has something to update. - Execution of Batch: The batch is executed within
Test.startTest()
andTest.stopTest()
to simulate the asynchronous execution of the batch job. This also helps in monitoring governor limits specific to the test context. - Results Verification: After the batch execution, it asserts that the descriptions of all accounts are updated as expected.
Batch Apex Limits
Batch Apex in Salesforce is designed to handle large data volumes by splitting tasks into manageable batches. However, to ensure system performance and fairness across all users, Salesforce imposes several limits specific to Batch Apex. Understanding these limits is crucial for designing efficient and robust batch processes.
Here is a table outlining the limits for Batch Apex in Salesforce:
Limit | Description |
---|---|
Maximum number of batch apex jobs | 5 queued or active jobs at a time |
Batch size | Up to 2000 records per batch |
Total number of records processed | 50 million records per 24-hour period |
Number of batch apex job starts | 250,000 per 24-hour period |
Maximum query rows returned | 50,000,000 rows per batch |
Total number of batches processed | 2,500 per 24-hour period (including execute and finish method calls) |
Maximum number of callouts per batch | 100 callouts per execute method |
Total CPU time per batch | 60,000 milliseconds (60 seconds) per execute method |
Total heap size per batch | 12 MB (for synchronous execution) and 6 MB (for asynchronous execution) per batch |
Maximum number of scheduled jobs | 100 jobs can be scheduled concurrently |
Maximum number of future calls | 50 calls per transaction |
These limits ensure optimal performance and resource usage when executing Batch Apex jobs in Salesforce.
Here are the key limits associated with Batch Apex:
1. Total Number of Batch Apex Jobs in the Apex Flex Queue
Maximum Jobs: You can have up to 100 batch jobs in the Apex flex queue as holding status. This allows you to submit batch jobs without worrying about hitting the limit of active batch jobs, which are batch jobs that are in the queue or currently executing.
2. Active Batch Apex Jobs
Concurrent Batches: You can have up to 5 batch jobs running concurrently (this limit is lower in some Salesforce instances like 2 in Developer Edition).
3. Batch Size
- Default Batch Size: When a batch job is submitted without specifying a batch size, Salesforce processes up to 200 records per batch.
- Maximum Batch Size: You can specify a maximum of 2,000 records to be processed in a batch. However, the recommended size depends on the complexity of the operations involved.
4. Governor Limits in Batch Apex
Since each execution of a batch’s execute
method is considered a discrete transaction, each transaction has its own set of governor limits:
- SOQL Queries: Up to 200 SOQL queries.
- DML Statements: Up to 150 DML statements.
- SOSL Queries: Up to 20 SOSL queries.
- Total Number of Records Retrieved by SOQL: 50,000 records.
- Total Number of Records Affected by DML: 10,000 records.
- Total Heap Size: 6MB synchronous, 12MB asynchronous.
- Maximum CPU Time: 10,000 milliseconds.
Read more about Governor limits in Salesforce.
5. QueryLocator Object Limits
QueryLocator Rows: A single QueryLocator in Batch Apex can contain up to 50 million records. This is the maximum number of records that can be returned in the start
method of a batch job.
6. Chain Jobs
Chaining Batches: You can chain one batch job to another from the finish
method. This allows continuous processing if you need to work with more data than can be processed in a single batch job. However, it’s important to handle the chaining carefully to avoid creating infinite loops or excessive chaining which can lead to unexpected org limits.
7. Callouts in Batch Apex
Callouts in Batch Apex allow external web service integration during batch processing. To perform callouts, implement the Database.AllowsCallouts
interface in your batch class. The execute
method can then include HTTP requests to external services. Each batch can make callouts, but ensure callout limits are respected. For example, a batch job could retrieve data from an external API and update Salesforce records accordingly. This capability is crucial for real-time data synchronization and integrating Salesforce with external systems.
Total Number of Callouts: Up to 100 callouts in each execute
method call, with a cumulative timeout limit of 120 seconds.
8. Asynchronous Apex Limits
Daily Asynchronous Apex Executions: Depending on your org’s license type, you may be allowed up to 250,000 or more asynchronous Apex method executions (batch Apex, future methods, Queueable Apex, and scheduled Apex) per 24 hours.
Best Practices
- Always perform tests in a controlled environment to understand how your batch jobs perform under these limits.
- Utilize
Database.Stateful
to preserve state across batches when necessary but be mindful of memory usage. - Monitor and log the execution of batch jobs to ensure they are performing as expected and to troubleshoot any issues.
CRS Info Solutions provides a top-tier Salesforce course for beginners, offering expert instruction and a solid foundation to help you achieve certification and excel in interviews. Join our Salesforce course program today, which includes 100% job placement assistance, and take advantage of our free demo to discover more. Our Salesforce course incorporates Trailhead for a comprehensive learning experience. Sign up for a free demo now!