Sub topic 5
Your asynchronous options
This chapter assumes you can write Apex classes and triggers, know the main governor limits, and understand HTTP requests and JSON. The earlier chapters explained why a trigger cannot call out directly. Here you will see what Apex offers in its place.
The idea behind all of them
Asynchronous Apex runs your code in a separate transaction, after the trigger's transaction has committed. The trigger finishes first. The platform then starts your job on its own schedule.
That separation is what makes a callout legal. The new transaction has no uncommitted database work pending, so it can open a connection to an external service.
Three tools are relevant to trigger callouts. Each one starts from the same place: the trigger hands off a small amount of data and returns.
- Future methods are static methods marked with
@future. - Queueable Apex is a class that implements
Queueable. - Batch Apex is a class that implements
Database.Batchable.
A fourth option, Scheduled Apex, runs code at set times. It is not a response to a record change, so this tutorial does not use it for trigger callouts.
Comparing the three
| Feature | Future method | Queueable | Batch |
|---|---|---|---|
| Declared as | `@future(callout=true)` static method | Class with `Queueable` and `Database.AllowsCallouts` | Class with `Database.Batchable` and `Database.AllowsCallouts` |
| Parameters | Primitives and collections of primitives only | Any serializable member variables, including sObjects | Defined by the constructor and the query or iterable |
| Returns a job Id | No | Yes, from `System.enqueueJob` | Yes, from `Database.executeBatch` |
| Chaining | Not allowed from another future | One job can enqueue another | `finish` can start another batch |
| Failure handling | Limited | Finalizers | `finish` method and per-chunk exceptions |
| Typical use | Simple, one-off callout | Most trigger callouts | Very large data volumes |
Limits that shape the choice
The async options share some limits, and these decide which tool fits.
- A single transaction can make 50 asynchronous calls to
System.enqueueJob. A trigger that enqueues once per record breaks this in a bulk load. - A transaction can make 50 future calls. Each future call also counts against a daily asynchronous Apex limit that is shared across the org.
- A future method cannot call another future method, and a trigger inside a future or batch context cannot start one either.
- A Queueable job run from a trigger can add only one child job to the chain.
These numbers can change between releases, so confirm them against the current Apex limits documentation before you rely on them.
The practical lesson: enqueue once per trigger invocation, not once per record. A later chapter covers this in detail.
Why Queueable is the usual starting point
Future methods are the older tool and are easy to write. They accept only primitive parameters, give you no job Id to track, and offer little control after the call.
Queueable Apex fixes those gaps. You get a job Id, richer state, chaining, and Finalizers for failure handling. For a trigger callout that must handle a whole batch of records, Queueable is the normal choice.
Batch Apex suits work that needs to process thousands of records in chunks. A trigger rarely needs it directly.
Exercise
A trigger on Account must send a bulk insert of 200 records to an external API, and you want a job Id for tracking and a way to react to failures. Which asynchronous option fits best, and why would you avoid a future method?
Show answer
Use Queueable Apex with Database.AllowsCallouts. It returns a job Id from System.enqueueJob and supports Finalizers for failure handling. A future method returns no job Id, accepts only primitive parameters, and offers little control over failures.