Salesforce Dictionary — Free Salesforce GlossarySalesforce Dictionary

Batch Apex

Development🔴 Advanced

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.

Real-World Example

a Salesforce developer at CodeBridge recently implemented Batch Apex to create a robust integration between Salesforce and an external system. Using Batch Apex, the developer builds an efficient solution that syncs data in near real-time, handles error scenarios gracefully, and includes detailed logging for troubleshooting.

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

  • CodeBridgeBuilt 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.
  • TerraForm TechUses 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.
  • Quantum LabsImplemented 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.

🧠 Test Your Knowledge

1. What is the maximum batch size in Batch Apex?

2. What are the three methods of the Database.Batchable interface?

3. Why can Batch Apex process millions of records when a single transaction cannot?

See something that could be improved?

Suggest an Edit