Sub topic 6
Calling out with a future method
This chapter assumes you can write Apex classes and bulk-safe triggers, know the basic governor limits, and have seen an HttpRequest callout. A trigger cannot call out synchronously, so the work moves to a future method. A future method is a static method that Salesforce runs later, in its own transaction, after your trigger's transaction commits.
How a future method fits the trigger flow
The trigger finishes and commits its database work first. Salesforce then runs the future method separately, so the callout no longer holds the original transaction open.
Writing the future method
The annotation must be @future(callout=true). Without callout=true, the platform rejects the callout inside the method.
public class AccountCalloutService {
@future(callout=true)
public static void syncAccounts(Set<Id> accountIds) {
List<Account> accounts = [SELECT Id, Name FROM Account WHERE Id IN :accountIds];
HttpRequest req = new HttpRequest();
req.setEndpoint('callout:Billing_API/accounts');
req.setMethod('POST');
req.setHeader('Content-Type', 'application/json');
req.setBody(JSON.serialize(accounts));
HttpResponse res = new Http().send(req);
System.debug('Status: ' + res.getStatusCode());
}
}The endpoint uses the Named Credential Billing_API, so the URL and authentication stay out of the code. Notice that the method takes Ids and queries the records itself. That is deliberate, and the rules below explain why.
Calling it from the trigger
The trigger makes one call for the whole batch, not one per record.
trigger AccountSyncTrigger on Account (after insert) {
AccountCalloutService.syncAccounts(Trigger.newMap.keySet());
}This is the same bulk idea as the Queueable version: collect the Ids, make a single call, and send one request that carries all the records.
Restrictions to remember
A future method has fixed rules.
- It must be
staticand returnvoid. - Its parameters must be primitives or collections of primitives. You cannot pass an sObject or a custom class instance.
- It runs when system resources allow, so you get no guarantee about timing or order.
- It returns no job Id, so you cannot track or cancel it directly.
- A future method cannot call another future method. Trigger code that does so from inside a future context throws an exception.
- One transaction can queue at most 50 future calls.
The parameter rule is also a safety feature. The records can change between the moment the trigger queues the call and the moment it runs. If you passed the sObjects themselves, the method would work with stale values. Passing Ids and re-querying gives you current data.
Common errors
| Symptom | Likely cause |
|---|---|
| "Callout from triggers are currently not allowed" | The trigger made the callout directly instead of through a future method. |
| Compile error about method signature | The method is not `static`, does not return `void`, or has an sObject parameter. |
| "Future method cannot be called from a future or batch method" | A future method was called from a future or batch context. Use a Queueable instead. |
| Callout fails inside the future method | `callout=true` is missing from the annotation. |
Where future methods fall short
A future method is a good fit for a single, simple callout. It gives you no job Id, no chaining, and no Finalizer for failure handling. When you need any of those, use Queueable Apex, which the next chapter covers.
Exercise
A developer writes @future public static void sync(List<Account> accounts) and makes a callout inside it. Name the two problems with this method.
Show answer
First, a future method cannot take a List of sObjects. Its parameters must be primitives or collections of primitives, so it should take a Set<Id> or List<Id> and re-query. Second, the annotation lacks callout=true, so the callout would be rejected at runtime.