Sub topic 13
Asynchronous Apex for long-running or high-volume work
This chapter assumes you can read basic Apex and SOQL. You should also know what governor limits are and how an Apex trigger runs on a record save.
Why asynchronous Apex exists
Synchronous Apex runs inside the user's save. It shares one transaction and one set of governor limits with every other automation on that record. If the work is slow, touches many records, or calls an external system, it can hit a limit or make the user wait.
Asynchronous Apex moves that work into a separate transaction. It runs later, with its own limits, and the user does not wait for it.
The main options
| Type | Best for | Key trait |
|---|---|---|
| Queueable | Follow-up work after a save, chained jobs, callouts | Accepts complex data, easy to enqueue from a trigger |
| Batch Apex | Processing very large data sets, up to millions of records | Splits records into chunks, each in its own transaction |
| Scheduled Apex | Running code at a set time | Often starts a batch or queueable job |
Queueable is the usual first choice. Use Batch Apex when the volume is too large for one transaction. Use Scheduled Apex when the trigger is the clock.
A Queueable example
The class below recalculates the rating for a list of accounts. It uses one query and one DML statement for the whole list, so it is bulk-safe.
public class RecalculateRatingJob implements Queueable {
private List<Id> accountIds;
public RecalculateRatingJob(List<Id> accountIds) {
this.accountIds = accountIds;
}
public void execute(QueueableContext context) {
List<Account> accounts = [SELECT Id, AnnualRevenue FROM Account WHERE Id IN :accountIds];
for (Account acc : accounts) {
acc.Rating = (acc.AnnualRevenue != null && acc.AnnualRevenue >= 1000000) ? 'Hot' : 'Warm';
}
update accounts;
}
}
// Enqueue from Apex, for example inside a trigger handler:
// System.enqueueJob(new RecalculateRatingJob(accountIdList));Enqueue the job once with a list of Ids. Do not enqueue one job per record inside a loop.
How the flow of work looks
Common errors
- Assuming the job runs immediately. It runs when system resources are available. Never write code that depends on the job having finished.
- Enqueuing inside a loop. A single transaction can enqueue only a limited number of jobs, so pass a list of Ids instead.
- Passing stale data. The job sees the database as it is when it runs. Pass Ids and query fresh values.
- Ignoring failures. The user never sees an error from an async job. Add logging or an alert, as covered in the testing and monitoring chapter.
- Reaching for async too early. If a before-save flow or a plain trigger can do the work, async adds complexity for no gain.
When to choose it
Pick asynchronous Apex when the work is slow, high in volume, or calls an external service, and the user does not need the result in the same save. If the result must appear immediately, keep it synchronous and reduce its cost instead.
Exercise
A trigger on Opportunity must call an external pricing service after each save, and the user does not need the response right away. Which async option fits first, and why should you pass record Ids rather than full records?
Show answer
Use a Queueable job (it supports callouts when it implements Database.AllowsCallouts) because it is simple to enqueue from a trigger and runs in its own transaction. Pass Ids so the job queries current data when it runs, instead of working with values that may be stale.