Skip to main content

Use of @future Method in Loop

David Martin avatar
Written by David Martin
Updated over a week ago

Overview

This rule flags Apex code where a method annotated with @future is called inside a loop. While @future methods are designed for asynchronous execution, invoking them in a loop can quickly exhaust Salesforce governor limits and introduce execution unpredictability.


Code Reviews classifies this as an Error because it severely impacts system performance, reliability, and scalability especially in bulk processing scenarios.


Why This Matters

Calling @future methods inside loops can:

  • Exceed the asynchronous call limit (only 50 @future calls allowed per transaction)

  • Cause runtime exceptions that may disrupt business processes

  • Lead to race conditions or inconsistent data states

  • Violate Salesforce best practices for bulk-safe asynchronous processing

This issue is especially critical in triggers, where processing multiple records is common.
​

What Triggers This Rule

Code Reviews flags any Apex code where:

  • A @future method is directly invoked within a for, while, or do-while loop

  • Each iteration of the loop potentially triggers a separate asynchronous call

Recommended Approach

Aggregate data outside the loop, then make a single @future call that handles all records in bulk.


Summary

Never call @future methods inside loops. Instead, collect all needed data first and invoke the method once with a bulk-safe parameter. Code Reviews flags this as an Error to help teams avoid governor limit violations and maintain scalable asynchronous logic.

Did this answer your question?