Sub topic 2
Declaring and Calling a Future Method
This chapter assumes you know basic Apex syntax (classes, static methods, collections), SOQL and DML, and the idea of governor limits. It covers how to declare a future method, how to call it, and the mistakes that most often break it.
What a future method is
A future method is a static method that Apex runs in its own transaction, after the current transaction finishes. You mark it with the @future annotation. The caller does not wait for the result and cannot receive a return value.
The declaration rules
A valid future method must follow these rules:
- It is declared
static. - It returns
void. - It carries the
@futureannotation. - Its parameters are primitives, arrays of primitives, or collections of primitives. It cannot take sObjects or other objects.
Here is a minimal example:
public class AccountService {
@future
public static void updateAccountRatings(Set<Id> accountIds) {
List<Account> accounts = [SELECT Id, Rating FROM Account WHERE Id IN :accountIds];
for (Account a : accounts) {
a.Rating = 'Hot';
}
update accounts;
}
}The method takes a Set<Id>, not a list of Account records. It then queries the accounts itself. This matters because the method may run seconds or minutes after the call. Any record you passed in would be stale by then. IDs let the method read the current data.
Calling a future method
You call a future method like any other static method:
Set<Id> ids = new Map<Id, Account>([SELECT Id FROM Account LIMIT 10]).keySet();
AccountService.updateAccountRatings(ids);The call does not run the method body. It places a request on a queue, and the platform runs the body later in a new transaction with fresh governor limits. The calling code continues immediately.
What happens at call time
The arguments are serialized when you make the call. If the calling code changes a collection after the call, the future method still sees the original values.
If the calling transaction fails and rolls back, the queued request is discarded, so the future method does not run. That is useful, because it will not act on data that never saved.
Future methods are not guaranteed to run in the order you called them. Do not write code that depends on one future call finishing before another starts.
Common errors
| Symptom | Cause | Fix |
|---|---|---|
| Compile error on the method signature | The method is not `static`, or does not return `void` | Make it `public static void` |
| Compile error on a parameter | You passed an sObject or a custom class | Pass IDs or primitives and re-query |
| Runtime error: callout not allowed | The method makes an HTTP callout but uses plain `@future` | Use `@future(callout=true)` |
| The method seems to do nothing in a test | The queued work has not run yet | Wrap the call in `Test.startTest()` and `Test.stopTest()` |
| Method acts on old field values | You passed values in rather than IDs | Re-query inside the method |
A pattern to follow
Keep the future method thin. Pass the smallest input that identifies the work, usually a set of IDs. Re-query inside the method, process in bulk, and handle failures there, because the caller can no longer see them.
Exercise
A developer writes @future public static void process(List<Account> accounts). The code does not compile. Name two problems and give the corrected signature.
Show answer
The method cannot accept a list of sObjects as a parameter. A future method also needs to be static, and this one is, so the only parameter problem is the sObject list. If the method were declared non-static, that would be a second error. The corrected signature is @future public static void process(Set<Id> accountIds), and the method re-queries the accounts by ID inside its body.
Exercise
A trigger calls a future method, and then the trigger's transaction throws an unhandled exception. Does the future method run? Why?
Show answer
No. The exception rolls back the transaction, and the queued future request is discarded along with it.