Sub topic 13
Choosing the right tool: a decision framework
This chapter assumes you can read basic Apex, write SOQL and DML, and know what a governor limit is. It also assumes you have seen the earlier chapters on @future, Queueable, Batch, and Scheduled Apex. Here you compare them and pick one.
Start with the requirement
Ask these questions in order. The first "yes" usually gives you the tool.
- Does the work need to run at a set time or on a repeating schedule? Use Scheduled Apex.
- Could it touch more records than one transaction can handle, such as tens of thousands to millions? Use Batch Apex.
- Does it need non-primitive parameters, a job Id, or sequential steps? Use Queueable Apex.
- Is it a small, one-off task that can take simple Ids, such as a callout after a DML operation? A @future method works. Queueable also works.
Compare the tools
| Need | future | Queueable | Batch | Scheduled |
|---|---|---|---|---|
| Parameters | Primitives and collections of primitives | Any serializable member variables | Query or iterable | None |
| Returns a job Id | No | Yes | Yes | Yes |
| Handles large volumes | No | Limited | Yes, in separate transactions | No, delegates |
| Sequential steps | No | Chain a child job | `finish` can start another job | No |
| Runs on a schedule | No | No | Only through a scheduler | Yes |
| Failure hook | None | Finalizer | `finish` method | None |
Apply the criteria
Data volume. One Queueable execution is one transaction, so it shares that transaction's limits. Batch gives every chunk of records a fresh set of limits. If the record count has no firm upper bound, choose Batch.
State. A future method cannot hold an sObject list. A Queueable can. A Batch class keeps state across execute calls only if it implements Database.Stateful.
Ordering. Chain Queueable jobs when step two depends on step one. Remember that a running Queueable can enqueue only one child job.
Failure handling. If you must react to a crash, such as logging or retrying, attach a finalizer to a Queueable. Future methods give you no such hook.
Capacity. Async work shares org-wide caps. Only a limited number of batch jobs can be queued or active at once, and the org has a daily async execution allocation. A design that starts a job per record can exhaust these even when no single transaction breaks a limit.
Worked example
The requirement: every night, flag leads untouched for a year, and there may be hundreds of thousands.
- Nightly means Scheduled Apex.
- The volume means Batch Apex.
- The scheduler should only start the batch, as you saw earlier:
public class NightlyStaleLeadJob implements Schedulable {
public void execute(SchedulableContext sc) {
Database.executeBatch(new StaleLeadBatch(), 200);
}
}A second requirement: after an account changes, post its name to a partner API. This is a single small unit of work with a callout. Queueable with Database.AllowsCallouts is the default choice. Use @future(callout=true) only if the code base already uses it and the inputs are plain Ids.
Exercise
Exercise
A trigger must send each new Opportunity to an external system. Typical inserts are 1 to 200 records, and the call needs the full Opportunity fields. Which tool fits, and why not the others?
Show answer
Queueable Apex with Database.AllowsCallouts. It accepts a list of sObjects as member state, so the full fields travel with the job, and it returns a job Id for monitoring. A future method cannot take sObjects. Batch is too heavy for a volume that a single transaction already handles. Scheduled Apex adds delay and is not tied to the insert event.