Sub topic 8
Governor Limits That Apply to Future Methods
This chapter assumes you can write Apex classes with static methods, run SOQL and DML, and have seen governor limits in a synchronous transaction. Here you will learn which limits apply to future methods. You will also learn how to stay under them, and what errors you get when you don't.
Two kinds of limits
Future methods are affected by two separate groups of limits.
- Per-transaction limits. The calling transaction has limits on how many future methods it may queue. Each future method then runs in its own transaction with its own limits.
- Org-wide limits. The org has a daily cap on asynchronous executions, plus a shared queue that Salesforce processes fairly across all customers.
Keep these two groups apart. Most mistakes come from mixing them up.
Limits on the code that queues future methods
The calling transaction may make at most 50 future method calls. The 51st call throws a System.LimitException with the message Too many future calls: 51.
The limit counts calls, not records. This is why a future call inside a loop is a classic mistake:
// 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, this fails on the 51st iteration. The whole transaction rolls back, including the work that had succeeded. Collect the IDs first and make one call, as in the trigger from the earlier chapters.
The calls you queue also use up the transaction's other budgets. Each call costs a little CPU time. The transaction does not wait for the future method, so none of the future method's work counts against the caller.
Checking the budget at run time
The Limits class reports how many future calls the current transaction has used and how many it may use:
public class RatingDispatcher {
public static void dispatch(Set<Id> accountIds) {
if (accountIds == null || accountIds.isEmpty()) {
return;
}
if (System.isFuture() || System.isBatch()) {
System.debug(LoggingLevel.WARN, 'Future calls not allowed in this context');
return;
}
if (Limits.getFutureCalls() >= Limits.getLimitFutureCalls()) {
System.debug(LoggingLevel.WARN, 'Future call limit reached; work not queued');
return;
}
AccountService.updateAccountRatings(accountIds);
}
}This guard has two jobs. It avoids the LimitException, and it makes the failure visible in the debug log. Silently skipping the work is only safe if something else can pick it up later. For critical work, queue a record or flag instead and let a scheduled job retry.
Limits inside a future method
A future method runs as its own transaction, and several of its limits are higher than the synchronous ones. Salesforce documents these as the "asynchronous" limits.
| Limit | Synchronous | Future (asynchronous) |
|---|---|---|
| SOQL queries issued | 100 | 200 |
| Total heap size | 6 MB | 12 MB |
| Maximum CPU time | 10,000 ms | 60,000 ms |
| Records returned by SOQL | 50,000 | 50,000 |
| DML statements | 150 | 150 |
| Records processed by DML | 10,000 | 10,000 |
| Callouts per transaction | 100 | 100 |
| Total callout time | 120 seconds | 120 seconds |
Check the current Apex Developer Guide for exact values. Salesforce changes limits between releases, and the numbers above are the ones that stay stable for future methods.
Three points follow from the table.
- More headroom is not unlimited. The higher CPU and heap limits help with heavy calculation or large JSON. They do not remove the need for bulk-safe code. A query inside a loop still fails at 200 queries.
- DML and row limits do not change. A future method that must update 50,000 records cannot do it in one call. That is a job for Batch Apex.
- Limits reset per call. Ten future calls give you ten fresh sets of limits. This is why splitting work across several future calls can seem to "avoid" limits. It is also what pushes you toward the 50-call cap and the daily cap.
Watching consumption from inside
You can log how close a future method comes to its limits. This helps you decide whether it needs redesign.
public class LimitReporter {
@future
public static void processAccounts(Set<Id> accountIds) {
List<Account> accounts = [SELECT Id, Name FROM Account WHERE Id IN :accountIds];
// ... real work here ...
System.debug(
'SOQL: ' + Limits.getQueries() + '/' + Limits.getLimitQueries() +
', CPU: ' + Limits.getCpuTime() + '/' + Limits.getLimitCpuTime() +
', Heap: ' + Limits.getHeapSize() + '/' + Limits.getLimitHeapSize()
);
}
}Read the getLimit... values from the running transaction instead of hard-coding numbers. The reported limit will be the asynchronous one inside a future method.
Org-wide limits
Daily asynchronous executions
Salesforce caps the number of asynchronous Apex executions in a rolling 24-hour period. The cap is the greater of:
- 250,000, or
- the number of user licenses in the org multiplied by 200.
The cap is shared. It counts future methods, Queueable Apex, Batch Apex (each batch chunk counts), and scheduled Apex together. A busy org can use its allowance through batch jobs and leave little for future methods.
Calls to a future method are counted when they run, not when they are queued. A 200-record trigger that queues one future call costs one execution. The same trigger with a per-record call costs 200, if it didn't fail first on the 50-call cap.
The shared queue
Future methods wait in a queue that serves many orgs. Salesforce documents that if more than 2,000 unprocessed requests from a single org are waiting, additional requests from that org are delayed while the queue serves other orgs. Your future methods still run, but possibly much later than you expect. This is one reason you cannot rely on a future method finishing within a set time.
Limits in tests
The 50-call cap applies in test code too. Inside a test, future calls do not run until Test.stopTest(). When the test ends, they run synchronously, and each one gets its own limits.
Because of this, a test that queues the maximum number of calls is a cheap way to confirm your guard works:
@isTest
private class RatingDispatcherTest {
@isTest
static void guardStopsBeforeFiftyFirstCall() {
Account a = new Account(Name = 'Limit Test');
insert a;
Test.startTest();
for (Integer i = 0; i < 60; i++) {
RatingDispatcher.dispatch(new Set<Id>{ a.Id });
}
Integer used = Limits.getFutureCalls();
Test.stopTest();
System.assertEquals(50, used, 'Dispatcher should stop at the 50-call cap');
}
}The loop makes 60 attempts. The guard stops queuing at 50, so the test passes without a LimitException.
Common limit errors
| Error message | Likely cause | Fix |
|---|---|---|
| `Too many future calls: 51` | Future method called inside a loop, or from several triggers in one transaction | Collect IDs and call once per transaction. Check `Limits.getFutureCalls()` before calling |
| `Future method cannot be called from a future or batch method` | Calling a future method from a context that does not allow it | Check `System.isFuture()` and `System.isBatch()`, or move to Queueable Apex |
| `Too many SOQL queries: 201` | Query inside a loop in the future method | Query once with `IN :ids` and group results in a map |
| `Apex heap size too large` | Serializing or loading too much data in one call | Pass fewer IDs per call, or process in Batch Apex |
| `Apex CPU time limit exceeded` | Heavy loops or complex logic, often combined with triggers that re-fire | Simplify the logic, or move the work to Batch Apex |
Remember that DML inside a future method can fire triggers. Those triggers run inside the future transaction and share its limits. If the trigger tries to queue another future call, the guard from the previous chapter prevents it.
Planning around the limits
Use these rules when you design a future method.
- Make one future call per transaction, with a set of IDs.
- Keep the future method bulk-safe. One query in, one DML out.
- Check
Limits.getFutureCalls()before queuing when the call comes from code that may run many times in one transaction. - Do not depend on speed. The daily cap and the shared queue both affect when your code runs.
- If one call must handle more than a few thousand records, or the work needs more than the asynchronous CPU and heap, use Batch Apex.
Exercise
A trigger on Contact fires for 150 records in one batch. It runs a loop that calls a future method once per contact. What happens, and how do you fix it?
Show answer
The loop fails on the 51st call with "Too many future calls: 51", and the whole transaction rolls back. Fix it by collecting the contact IDs in a Set<Id> inside the loop and making a single future call after the loop. Optionally check Limits.getFutureCalls() against Limits.getLimitFutureCalls() before calling.
Exercise
A future method needs to update 40,000 Account records. The CPU time is fine. Is a future method a good choice, and why?
Show answer
No. DML is limited to 10,000 records per transaction even in a future method, because the asynchronous context raises CPU, heap, and SOQL query limits but not the DML row limit. Batch Apex fits better because it splits the work into chunks, and each chunk gets its own limits.
Exercise
Your org used most of its 24-hour asynchronous allowance on nightly Batch Apex jobs. Do future methods count against the same allowance?
Show answer
Yes. The daily cap is shared by future methods, Queueable Apex, Batch Apex (each chunk), and scheduled Apex, so heavy batch use leaves fewer executions for future methods.