Sub topic 4
Feeding the job: QueryLocator or Iterable
This chapter assumes you can write the three methods of a Database.Batchable class and know basic SOQL. Here you choose what the start method returns, because that choice decides how many records the job can handle.
The two return types
start runs once and returns the full set of work. It can return one of two types:
| Return type | Source of records | Row limit |
|---|---|---|
| `Database.QueryLocator` | A SOQL query | Up to 50 million records |
| `Iterable` | Any Apex collection or custom iterator | Bound by the limits of the `start` transaction |
QueryLocator: the default choice
A QueryLocator hands the query to the platform instead of running it inside start. The 50,000-row SOQL limit does not apply to it. The platform then reads the matching records in chunks and passes each chunk to execute.
public Database.QueryLocator start(Database.BatchableContext bc) {
return Database.getQueryLocator([
SELECT Id, Rating FROM Account WHERE Rating = null
]);
}You can also build the query from a string with Database.getQueryLocator(String). If you do, bind variables must be fields or local variables that are in scope in start, and you are responsible for avoiding SOQL injection.
Iterable: when the work is not a single query
Use an Iterable when the records do not come from one query. Examples are a list of strings from a callout, records filtered in Apex code, or items from several objects. The type parameter on the class sets the type of execute's scope list.
public class DomainReportBatch implements Database.Batchable<String> {
private List<String> domains;
public DomainReportBatch(List<String> domains) {
this.domains = domains;
}
public Iterable<String> start(Database.BatchableContext bc) {
return domains;
}
public void execute(Database.BatchableContext bc, List<String> scope) {
for (String domain : scope) {
System.debug('Processing ' + domain);
}
}
public void finish(Database.BatchableContext bc) {
System.debug('Done: ' + bc.getJobId());
}
}A List is already an Iterable, so you can return it directly. For lazy loading, write a class that implements Iterable<T> and returns an Iterator<T> with hasNext() and next().
Choosing between them
Common errors
- Wrong generic type. If the class implements
Database.Batchable<SObject>,executemust acceptList<SObject>. DeclaringList<Account>still compiles in a typed class, but it fails when the class is declared withStringor a custom type. - Query too selective or too broad. The query runs when the job starts. Records created after that moment are not in the job.
- Large Iterable. Building a list of a million records in
startexceeds the heap limit. Switch to aQueryLocator, or have the iterator load items lazily. - Unfiltered queries. Add a
WHEREclause so a re-run skips records that are already processed.
Exercise
A batch job must process 20 million Contact records that match one SOQL filter. Which return type should start use, and why?
Show answer
Use a Database.QueryLocator. The platform runs the query outside the normal 50,000-row SOQL limit and supports up to 50 million records. An Iterable built from the same query would hit the governor limits of the start transaction.