Sub topic 8
Governor limits that apply
This chapter assumes you can write Apex classes and triggers, run SOQL and DML, and already know that Salesforce enforces governor limits per transaction. It covers the limits that matter when you use future methods, and the errors you see when you hit them.
Two sets of limits
A future method involves two transactions, and each has its own limits.
- The calling transaction has its normal synchronous limits. It also has a cap on how many future calls it can make.
- The future method runs in its own transaction. That transaction gets the asynchronous limits, which are higher for some resources.
Calling a future method does not use up the caller's SOQL or DML budget. The only cost to the caller is one count against the future call limit.
Limits to remember
The values below are the standard ones at the time of writing. Check the current Apex Developer Guide before you rely on them.
| Limit | Synchronous | Future method |
|---|---|---|
| Future calls per Apex invocation | 50 | 0 in a future method (with narrow exceptions, see below) |
| SOQL queries | 100 | 200 |
| Heap size | 6 MB | 12 MB |
| CPU time | 10,000 ms | 60,000 ms |
| DML statements | 150 | 150 |
| Callouts | 100 | 100 |
There is also an org-wide daily limit. An org can run at most 250,000 asynchronous Apex executions per 24 hours, or the number of user licenses multiplied by 200, whichever is greater. Future methods, Queueable, Batch, and scheduled Apex all count toward this total.
The 50-call limit
One transaction can make at most 50 future calls. The limit counts calls, not records. This code reaches the limit quickly when it processes many records:
// Anti-pattern: one future call per record
for (Account a : Trigger.new) {
AccountService.updateAccountRatings(new Set<Id>{ a.Id });
}With 200 records in a trigger batch, the 51st call throws a System.LimitException. The fix is to pass all the IDs in one call, as you saw in the trigger example in the previous chapter's pattern. This is also why a future method takes a collection of IDs.
Set<Id> ids = new Set<Id>();
for (Account a : Trigger.new) {
ids.add(a.Id);
}
AccountService.updateAccountRatings(ids);Limits inside the future method
The future method is bulk code like any other, so the usual rules apply there.
- Query once, outside the loop, as in
updateAccountRatings. - Run one DML statement on a list, not one per record.
- Keep serialized parameters small. Heavy work belongs in the method body, not in the arguments.
public class AccountService {
@future
public static void updateAccountRatings(Set<Id> accountIds) {
// One query and one DML statement, however many IDs arrive
List<Account> accounts = [SELECT Id, Rating FROM Account WHERE Id IN :accountIds];
for (Account a : accounts) {
a.Rating = 'Hot';
}
update accounts;
}
}Common errors
- Too many future calls: 51. A call sits inside a loop. Collect IDs first and make one call.
- Daily async limit reached. Many small future calls from high-volume triggers add up. Combine work into fewer calls, or move to Batch Apex.
- Future method cannot be called from a future or batch method. A future method cannot start another future method, and the call is blocked from batch contexts. Check
System.isFuture()andSystem.isBatch()before you call. The chapter on calling contexts covers this. - Heap or CPU limit inside the method. Large payloads, such as
JSON.serializeon many records, can hit these. Select only the fields you need.
Checking usage in code
The Limits class reports consumption in the current transaction.
System.debug('Future calls used: ' + Limits.getFutureCalls()
+ ' of ' + Limits.getLimitFutureCalls());
System.debug('SOQL queries used: ' + Limits.getQueries()
+ ' of ' + Limits.getLimitQueries());Inside a future method, these numbers describe the future transaction's own usage, not the caller's.
Exercise
A trigger on Contact calls a future method once per record. A data load inserts 200 contacts in one batch and fails with a LimitException. What is the cause, and how do you fix it?
Show answer
The trigger makes 200 future calls, and one transaction may make only 50. Collect the Contact IDs into a Set<Id> inside the trigger and call the future method once with that set. The future method then queries the records once and performs one DML statement.
Exercise
A future method runs a SOQL query. Does that query count against the 100-query limit of the transaction that called it?
Show answer
No. The future method runs in its own transaction with its own limits (200 queries). The caller only spends one of its 50 future calls.