All articles

Platform

Too many future calls: 51 in Apex

Replace per-record future calls with bounded work, understand execution-context limits and design observable asynchronous recovery.

A trigger that calls a future method for each record can fail before any background work starts. The dispatch loop consumes the caller’s allowance.

The governor reference lists 50 future invocations in an ordinary synchronous transaction, with different restrictions in other execution contexts. Don’t treat the number as permission to dispatch 50 jobs for every save. Other automation may use the same transaction’s resources.

Consolidate the caller first

Collect IDs in a set, then dispatch one bounded operation outside the loop. Future methods accept primitive values and collections of primitives, and Salesforce recommends Queueable Apex for new work that benefits from job IDs and chaining.

A consolidated future call still runs its work in one transaction. Passing thousands of IDs fixes the enqueue count while potentially creating query, callout or DML failures in the worker. Choose the worker’s scope from the work per record.

If you retain an existing future implementation, query the records when it executes and decide how to handle changes since the original request. A record may have been deleted, reassigned or moved to a different business state while waiting.

Avoid recursive asynchronous dispatch

A future method cannot call another future method, including indirectly through a trigger that invokes one. Trace what the background DML starts. A guard that checks execution context may prevent the exception but can silently omit required work if there is no alternative path.

Prefer separating the business operation from its dispatcher. The operation can accept an explicit bounded input; the dispatcher decides whether to run it now, enqueue it or place it in a durable work queue. That separation makes the transaction boundary easier to test.

For new Queueable work, preserve the returned job ID and expose status to an operator. If external side effects are involved, use stable operation identifiers so retries can be reconciled rather than duplicated.

Test dispatch and completion separately

A bulk test should show that a collection of source records produces the intended number of jobs, not one job per record. Test updates that do not require synchronization as well, so ordinary saves don’t create unnecessary work.

Use Test.startTest() and Test.stopTest() around the asynchronous work you need the test to execute. Don’t assume that one test proves every downstream chained job has run; test worker logic and dispatch decisions independently.

Finally, define what happens when the worker fails after the original save has committed. Keep the failed operation available for investigation and retry. A successful foreground save is not evidence that the asynchronous integration completed. The async Apex guide compares the main execution options.

Keep reading

All articles