Batch Apex
By the end of this lesson, you'll be able to:
- Implement the Batchable interface's three methods
- Explain how Batch Apex processes records in chunks automatically
- Recognize when Batch Apex fits better than a Queueable job
Prerequisites: "Queueable Apex"
The three required methods
public class UpdateStaleAccountsBatch implements Database.Batchable<sObject> {
public Database.QueryLocator start(Database.BatchableContext context) {
return Database.getQueryLocator([SELECT Id, Status__c FROM Account WHERE LastActivityDate < LAST_N_DAYS:365]);
}
public void execute(Database.BatchableContext context, List<Account> scope) {
for (Account acc : scope) {
acc.Status__c = 'Inactive';
}
update scope;
}
public void finish(Database.BatchableContext context) {
System.debug('Batch job complete.');
}
}
public class UpdateStaleAccountsBatch implements Database.Batchable<sObject> {— the class, implementing the required interface.public Database.QueryLocator start(Database.BatchableContext context) {— defines what to process.return Database.getQueryLocator([SELECT Id, Status__c FROM Account WHERE LastActivityDate < LAST_N_DAYS:365]);— potentially thousands of matching Accounts.}— closesstart.- (blank line)
public void execute(Database.BatchableContext context, List<Account> scope) {— runs once per chunk of records.for (Account acc : scope) {— iterates just this chunk.acc.Status__c = 'Inactive';— Module 25's bulk-safe update logic.}— closes theforloop.update scope;— one DML statement per chunk, not per record.}— closesexecute.- (blank line)
public void finish(Database.BatchableContext context) {— runs once, after every chunk has completed.System.debug('Batch job complete.');— placeholder logic.}— closesfinish.}— closes the class.
start defines what to process (here, every stale Account, potentially thousands); execute runs once per chunk of records (200 by default) — this is where the actual bulk-safe update logic (Module 25) lives; finish runs once, after every chunk has completed.
How chunking solves the scale problem
If start returns 50,000 matching Accounts, execute doesn't receive all 50,000 at once — the platform automatically splits them into chunks of (by default) 200 records each, calling execute once per chunk, each chunk getting its own fresh transaction with its own full governor limit budget. This is exactly what makes Batch Apex suited to genuinely large data volumes that no single transaction (synchronous or otherwise) could process at once.
Starting the batch job
Database.executeBatch(new UpdateStaleAccountsBatch(), 200);
Database.executeBatch(new UpdateStaleAccountsBatch(), 200);— the second argument sets the chunk size explicitly.
The second argument (200 here) sets the chunk size explicitly — a smaller chunk size means more total transactions (each with fresh limits, useful if execute does heavy per-record work); a larger one means fewer, bigger transactions.
Exercise
Write the skeleton of a Batchable class ArchiveOldCasesBatch processing every closed Case older than 2 years, with start querying them and execute debugging each Case's Id.
Show hint
LAST_N_DAYS:730 in the query, following the three-method shape.
Batch Apex Quiz
My Notes
Log in to keep private notes on this lesson.
Questions about this lesson
No questions yet — be the first to ask.
Log in to ask a question about this lesson.
Summary
Batch Apex processes a genuinely large number of records — potentially millions — by automatically splitting them into chunks, each running as its own transaction with fresh governor limits.