Sub topic 3
Future methods: simple, fire-and-forget work
This chapter assumes you know basic Apex syntax, SOQL and DML, and how triggers run inside a synchronous transaction. You should also know what governor limits are and how to write a basic unit test.
What a future method is
A future method is a static Apex method that Salesforce runs later, in its own transaction. You mark it with the @future annotation. The caller does not wait for it and gets no return value. This is why it is called fire-and-forget.
Use a future method when a transaction needs to do something it cannot do in place. Two common cases are making an HTTP callout after DML has already happened, and moving a small piece of slow work out of a trigger.
The restrictions
Future methods are the simplest async tool, and the restrictions are what you pay for that.
- The method must be
static. - The method must return
void. - Parameters must be primitives (such as
Id,String,Integer) or collections of primitives. You cannot pass an sObject or a custom class instance. - You cannot chain one future method from another future method.
- You get no job Id back, so you cannot monitor an individual call easily.
- Execution order is not guaranteed. Two future calls may run in either order.
Passing Ids instead of sObjects is deliberate. The method may run seconds or minutes later, and a record passed by value could be stale by then. Query it again inside the method to get current data.
Example: a callout from a future method
Salesforce does not allow a callout after uncommitted DML in the same transaction. A future method runs in a new transaction, so it avoids that problem. You must set callout=true to permit the HTTP request.
public class PartnerSync {
@future(callout=true)
public static void sendAccountNames(Set<Id> accountIds) {
List<Account> accts = [SELECT Id, Name FROM Account WHERE Id IN :accountIds];
HttpRequest req = new HttpRequest();
req.setEndpoint('callout:Partner_API/accounts');
req.setMethod('POST');
req.setBody(JSON.serialize(accts));
HttpResponse res = new Http().send(req);
System.debug(res.getStatusCode());
}
}The endpoint uses a Named Credential (callout:Partner_API), so the URL and authentication live in setup and not in code. The method receives a Set<Id> and queries the accounts itself.
Call it like any static method:
PartnerSync.sendAccountNames(new Set<Id>{ acct.Id });Limits that apply
A future call is queued when the line runs, and the work happens later with higher async governor limits (for example, more heap and CPU time than a synchronous transaction). Two limits matter most when you design around future methods:
- A single transaction can make at most 50 future calls.
- The org has a daily cap on async Apex executions. Future calls count toward it.
You also cannot call a future method from a batch or another future method. If a trigger might run in those contexts, check System.isFuture() and System.isBatch() before you call.
How a future call flows
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Compile error on the method signature | Non-static method, non-void return, or an sObject parameter | Make it `static void` and pass Ids |
| Callout fails with a "callout not allowed" error | Missing `callout=true` | Use `@future(callout=true)` |
| "Too many future calls: 51" | Future call inside a loop | Batch the Ids into one call |
| Method sees old field values | Data was passed by value, or changed before the method ran | Pass Ids and re-query |
| Future call fails from a future or batch context | Future calls are not allowed there | Guard with `System.isFuture()` and `System.isBatch()`, or use Queueable |
When to move on
Future methods fit small, independent tasks where you do not need a result, state, or ordering. If you need to pass an sObject, track the job, or run steps one after another, use Queueable Apex, which the next chapter covers.
Exercise
A trigger on Account must send each saved account's Name to an external service. A developer wrote @future public void send(List<Account> accts) and calls it once per record in a for loop. Name the problems in the signature and in the call pattern, and describe the fix.
Show answer
The signature has three problems. The method is not static, and the parameter is a list of sObjects, which is not allowed because future methods accept only primitives or collections of primitives. It also lacks callout=true, so the HTTP request would fail. The call pattern risks the 50-future-calls-per-transaction limit because it calls once per record. The fix is @future(callout=true) public static void send(Set<Id> accountIds). The trigger should collect all Ids and call it once, and the method should query the accounts itself.