
While Salesforce Flow is widely used for automation, handling loops efficiently within flows remains a challenge for most admins and developers. Poorly designed loops can cause governor limit violations or lead to CPU timeouts, especially when handling large datasets. In this guide, we will see how to handle loops in Salesforce Flow, avoid governor limits, and maximize performance.
- Use Assignment Outside the Loop:
Instead of performing DML operations inside the loop, store all records in a collection and update them at once after the loop finishes.
How to Do It:
- Create a record collection variable.
- Use “Assignment” to add records to the collection.
- Perform the DML operation (Create, Update, Delete) after the loop ends.
Example:
- Get all Opportunity records with Stage = “Proposal”.
- Loop through the records.
- Assign new values to a collection.
- Update all records using “Update Records” after the loop.
Why It Works:
- Reduces SOQL and DML operations → Fewer governor limits.
- Improves performance → Reduces CPU time.
- Use a Formula Instead of Decision Inside the Loop:
Instead of using multiple “Decision” elements inside the loop, create a formula field to simplify the logic.
How to Do It:
- Create a formula field with IF conditions.
- Reference the formula field instead of adding multiple decision branches inside the loop
Example:
If a formula field checks if Opportunity Amount > $10,000 → Create a single decision using the formula.
Why It Works:
- Reduces processing time.
- Simplifies the logic → Fewer elements to execute.
- Use Collection Filters Instead of Complex Looping
Instead of applying logic within the loop, use a collection filter to narrow down the data set before starting the loop.
How to Do It:
- Use “Filter Collection” before starting the loop.
- Process only the filtered records
Example:
- Get all cases created this week.
- Filter out closed cases before entering the loop.
Why It Works:
- Reduces loop size.
- Lowers CPU processing time.
- Use “Get Records” Only Once
Instead of querying records multiple times inside the loop, query all records at once and store them in a collection.
How to Do It:
- Use “Get Records” before the loop.
- Store results in a collection variable.
- Reference the collection inside the loop.
Example:
- Get all Accounts.
- Store them in a collection.
- Loop through the collection to update values.
Why It Works:
- Reduces SOQL queries.
- Improves flow execution time.
Example: Efficient Loop Handling in a Flow
Scenario:
A company needs to update 5,000 Opportunity records to update the “Stage” field based on the amount and close date.
Challenges:
- SOQL limit: 50,000 records
- DML limit: 150 statements
- CPU timeout: 10 seconds
Solution:
- Get all Opportunity records using a single “Get Records.”
- Use a “Collection Filter” to reduce the record count to <2,000
- Use “Assignment” to store updates in a collection variable.
- Perform a single “Update Records” action after the loop.
- If more than 2,000 records → Use a scheduled path for batching.
Why This Works:
- Only one SOQL query is executed → Stays within limit.
- One DML statement for multiple records → Stays within limit.
- Reduced CPU time by handling logic outside the loop
- Avoids hitting the 2,000 record limit by batching.
Common Pitfalls to Avoid:
- Placing DML actions directly inside loops à Leads to governor limit violations.
- Executing multiple “Get Records” inside loops à Causes too many SOQL queries.
- Lack of batching à Causes CPU timeout or data truncation.
- Ineffective filter usage à Processes too many records unnecessarily