Batch Apex
Batch Apex is an Apex programming pattern that allows developers to process large volumes of records asynchronously by breaking them into manageable chunks (batches) of up to 2,000 records each.
Definition
Batch Apex is an Apex programming pattern that allows developers to process large volumes of records asynchronously by breaking them into manageable chunks (batches) of up to 2,000 records each. A Batch Apex class implements the Database.Batchable interface and defines three methods: start (to collect records), execute (to process each batch), and finish (to perform post-processing). Batch Apex jobs have higher governor limits than synchronous transactions.
In plain English
“Batch Apex is a way to process huge amounts of records without breaking Salesforce. Instead of trying to handle a million records at once (which would hit limits fast), Batch Apex cuts the work into chunks of up to 2,000 records and processes them one chunk at a time in the background.”
Worked example
Catalpa Studios needs to recalculate royalty payments for 2.3 million order line items every month. A synchronous Apex job would hit governor limits in 2 seconds. The developer writes a Batch Apex class that implements Database.Batchable: the start() method returns a SOQL QueryLocator across the order lines; execute() processes each batch of 2,000 records (the standard batch size) and updates royalty fields; finish() sends a summary email. The job runs asynchronously over ~20 minutes overnight, processing all 2.3M records without touching governor limits in any single transaction. Batch Apex is the platform's pattern for large-volume background processing.
Why Batch Apex matters
Batch Apex is the Apex pattern for processing large record volumes asynchronously without hitting governor limits. A Batch Apex class implements the Database.Batchable interface with three methods: start (which returns the full set of records to process, typically via a QueryLocator that can handle up to 50 million records), execute (which processes a single batch of up to 2,000 records at a time), and finish (which runs after all batches complete, typically to send completion notifications or kick off follow-up work).
Because each batch executes in its own transaction, each batch gets its own governor limits. This means a Batch Apex job can process millions of records total, even though any single transaction could only handle a few thousand. Batch jobs are queued and executed by the platform, and their status can be monitored through the Apex Jobs page in Setup. Common use cases include nightly data cleansing, mass record updates after configuration changes, and calculating derived values across large datasets.
How organizations use Batch Apex
Built a Batch Apex job that recalculates a custom health score field on all Account records nightly. The job processes about 500,000 Accounts in batches of 200, taking roughly 30 minutes to complete, and logs any errors to a custom object for review.
Uses Batch Apex to migrate data after a schema change. When a new required field was added to Opportunity, a one-time Batch Apex job populated the field on millions of existing records based on derived logic from related objects.
Implemented a Batch Apex job that runs every Sunday to archive completed Cases older than two years to a custom archive object. The job respects governor limits by using a small batch size and efficient SOQL queries.
Trust & references
Straight from the source - Salesforce's reference material on Batch Apex.
- Batch ApexSalesforce Developers
- Use Batch ApexSalesforce Developers
Test your knowledge
Q1. What is the maximum batch size in Batch Apex?
Q2. What are the three methods of the Database.Batchable interface?
Q3. Why can Batch Apex process millions of records when a single transaction cannot?
Discussion
Loading discussion…