Batch scope size above the 2000-record maximum
ID |
apex.performance.batch_scope_size |
Severity |
high |
Remediation Complexity |
trivial |
Remediation Risk |
low |
Remediation Effort |
low |
Resource |
Performance |
Language |
Apex |
Description
Reports a Database.executeBatch(job, scope) call whose scope argument is greater than 2000.
Rationale
2000 is the platform’s maximum batch size, and a larger value is not clamped - the call throws at runtime, so the job never starts. Nothing catches it in the scheduler that enqueued it, and a scheduled job that fails to enqueue leaves no half-finished work to notice: the nightly run simply did not happen, and the first sign is the data it should have written being a day stale. A scope near the maximum is also rarely what the job wants, since each chunk gets one set of governor limits to share.
The following code illustrates the pattern detected by this rule:
public class NightlyJobs {
public void runTooWide() {
// FLAGGED: Batch scope size above the 2000-record maximum
Database.executeBatch(new AccountRollupBatch(), 5000);
}
public void runWayTooWide() {
// FLAGGED: Batch scope size above the 2000-record maximum
Database.executeBatch(new ContactRollupBatch(), 10000);
}
public void runDefault() {
Database.executeBatch(new AccountRollupBatch());
}
public void runNarrow() {
Database.executeBatch(new AccountRollupBatch(), 200);
}
public void runAtTheLimit() {
Database.executeBatch(new AccountRollupBatch(), 2000);
}
}