Sub topic 13
When to Use Queueable or Batch Apex Instead
This chapter assumes you can write and call a basic @future method, and that you know the limits covered earlier: primitive-only parameters, no chaining, and a cap on future calls per transaction. Here you decide when a future method is the wrong tool.
What future methods cannot do
Future methods are simple, but they have gaps:
- Parameters must be primitives or collections of primitives. You cannot pass an sObject or a custom class instance.
- They return
void, so the caller gets no result and no job ID to track. - A future method cannot call another future method. You cannot build a sequence of steps.
- You cannot call one from a future or batch context. That is why the trigger example checked
System.isFuture()andSystem.isBatch(). - You cannot control the order in which queued future methods run.
When one of these gaps blocks you, switch to Queueable or Batch Apex.
Queueable Apex
A Queueable class implements Queueable. It fixes most of the future method gaps:
- It accepts non-primitive members, such as sObjects and custom types, as class fields.
System.enqueueJob()returns a job ID. You can use it to look up the job inAsyncApexJob.- A running Queueable can enqueue one more Queueable, so you can chain steps.
public class RatingUpdater implements Queueable {
private List<Account> accounts;
public RatingUpdater(List<Account> accounts) {
this.accounts = accounts;
}
public void execute(QueueableContext context) {
for (Account a : accounts) {
a.Rating = 'Hot';
}
update accounts;
}
}Enqueue it from any Apex code:
List<Account> accounts = [SELECT Id, Rating FROM Account LIMIT 10];
Id jobId = System.enqueueJob(new RatingUpdater(accounts));
System.debug('Queued job: ' + jobId);Because the sObjects are serialized when you enqueue, the data can be stale by the time the job runs. For records that may change, pass IDs and re-query, as you did in a future method.
To make callouts, add Database.AllowsCallouts to the implements list.
Batch Apex
Batch Apex processes large data sets in slices. A class implements Database.Batchable<SObject> with three methods: start defines the records, execute handles one slice (200 records by default), and finish runs once at the end. Each slice runs in its own transaction with fresh governor limits, so a batch can process millions of records. A future method runs in one transaction and cannot do that.
Choosing between them
| Need | Future | Queueable | Batch |
|---|---|---|---|
| Quick fire-and-forget task | Best fit | Works | Overkill |
| Pass sObjects or custom types | No | Yes | Yes (through the query or class fields) |
| Job ID for monitoring | No | Yes | Yes |
| Chain follow-up steps | No | Yes | Yes (start another job in `finish`) |
| Process very large data volumes | No | No | Yes |
Common mistakes
- Choosing Batch Apex for a small job. Batch jobs wait in the flex queue and add overhead.
- Passing stale sObjects into a Queueable when the records change quickly.
- Enqueueing more than one Queueable from inside a running Queueable. Only one child job is allowed per execution.
- Forgetting
Database.AllowsCalloutsand then hitting a callout error.
Exercise
A trigger must send 5 Account records to an external API, and afterward a second step must run that uses the first step's result. Which tool fits, and why not a future method?
Show answer
Queueable Apex with Database.AllowsCallouts. The first job can enqueue a second Queueable for the follow-up step. A future method cannot call another future method, so it cannot chain the steps, and it returns no job ID to track.