Introduction to Salesforce Batch Apex [Video]
Explore core concepts and practical applications of Batch Apex in Salesforce, including the structure, how to write unit tests, scheduling, and more.
Join the DZone community and get the full member experience.
Join For FreeSalesforce Batch Apex is a powerful tool for handling large data volumes and complex data processing tasks asynchronously. This tutorial will walk you through the core concepts and practical applications of Batch Apex in Salesforce, including the structure of an Apex batch class, writing unit tests for batch classes, scheduling batch classes, running batch classes ad hoc for testing, and understanding Batch Apex limits.
Structure of an Apex Batch Class
An Apex Batch class in Salesforce must implement the Database.Batchable
interface. This interface requires the implementation of three methods:
- Start method: This method collects the records or objects to be passed to the
execute
method. It can return either aDatabase.QueryLocator
object or an iterable list of records. - Execute method: This method contains the logic to process each batch of data passed from the
start
method. It is called for each batch of records. - Finish method: This method is called after all batches are processed. It can be used to send notifications or perform any post-processing tasks.
Apex Batch Class Unit Test
Testing batch classes is crucial to ensure they work as expected. A unit test for a batch class typically involves:
- Setup test data: Creating the necessary test data within the test class
- Test the batch class: Calling the batch class within a test method and verifying the results; The test ensures that the batch class processes the records correctly and updates them as expected.
Batch Class Scheduling
Salesforce allows you to schedule a batch class to run at specific times using the System.schedule
method. To do this, you need to:
- Implement the
Schedulable
interface: Create a class that implements theSchedulable
interface and schedules the batch job. - Schedule the batch job: Use the
System.schedule
method to set the desired schedule. This involves defining a cron expression that specifies when the job should run. Alternatively, just use the front end with the scheduled jobs menu in Setup, as I've shown in the video below.
Running Batch Classes Ad Hoc for Testing
To run a batch class ad hoc for testing purposes, you can directly call the Database.executeBatch
method. This allows you to run the batch job immediately without scheduling it, making it useful for development and testing.
Batch Class Apex Limits
Understanding the limits is crucial when working with Batch Apex to avoid governor limit exceptions:
- Batch size: The maximum batch size is 2,000 records, but the optimal size is typically 200 to 500 records, depending on the complexity of the batch process.
- Total number of Batch Apex Jobs: Salesforce allows up to 5 active batch jobs simultaneously and up to 100 queued or active jobs.
- Total number of records processed: A single batch job can process up to 50 million records.
- Governor limits: Batch Apex execution is bound by various governor limits, such as the total number of DML statements, SOQL queries, CPU time, heap size, etc.
Video Tutorial
Conclusion
Salesforce Batch Apex is an essential feature for handling large-scale data processing efficiently. By understanding the structure of a batch class, writing comprehensive unit tests, scheduling batch jobs, running them ad hoc, and being mindful of the Apex limits, you can leverage Batch Apex to its fullest potential. Use this tutorial as a starting point to build and optimize your batch processes in Salesforce.
Feel free to reach out with any questions or further insights on Batch Apex in Salesforce. Happy coding!
Opinions expressed by DZone contributors are their own.
Comments