Salesforce Flow Loops: Best Practices and Practical Examples

Introduction
Salesforce Flow is a powerful automation tool that allows admins and developers to automate complex business processes without writing code. One of its most useful features is the ability to implement loops, which enable repetitive actions on a collection of records (such as lists or sObjects).
Table of Contents
1. Types of Loops in Salesforce Flow
Salesforce Flow provides two primary ways to loop through records:
A. Loop Element in Screen Flows
The Loop element is available in Screen Flows and allows users to iterate through a collection of records while displaying them on a screen.
Use Cases:
- Displaying a list of records for user selection.
- Collecting input for multiple records in a single screen.
Example:
A flow that lets a sales rep select multiple opportunities to update in bulk.
B. Assignment + Decision + Loop Logic in Autolaunched Flows
Salesforce does not have a built-in Loop element, but you can simulate looping behavior using
- Get Records (to retrieve a collection).
- Assignment (to track the current item and index).
- The decision is made to determine whether there are more items available.
- Loop Logic (using a variable to iterate).
Use Cases:
- Bulk updating records.
- Processing records from a query.
- Applying conditional logic to each record in a list.
Example:
A flow that updates the status of all open cases to “escalated” if they are older than 7 days.
2. Best Practices for Using Loops in Flow
To ensure efficient and error-free looping in Salesforce Flow, follow these best practices:
A. Optimize Query Performance
- Use filters in Get Records to reduce the number of records processed.
- Avoid querying unnecessary fields.
B. Avoid Infinite Loops
- Always include an exit condition (e.g., a counter variable).
- Set a maximum iteration limit (e.g., 100 loops) to prevent CPU timeouts.
C. Use Collections Efficiently
- Store records in a collection variable to avoid repeated queries.
- Use assignment elements to modify records in bulk.
D. Bulkify Your Flow
- Ensure your flow handles 200+ records efficiently.
- Use bulk DML operations (e.g., update all records at once instead of one by one).
E. Debug with System Debugs
- Use debug logs to track loop iterations.
- Log variables to identify issues.
3. Practical Examples of Flow Loops
Let’s explore real-world examples of loops in Salesforce Flow.
Example 1: Bulk Update Opportunities
Scenario: Update the stage of all opportunities in a list to “Closed Won.”
Steps:
1. Get Records: Retrieve all Opportunities where StageName != ‘Closed Won’.
2. Assignment: Store them in a collection variable.
3. Loop Logic:
- Use a counter variable (e.g., Index = 0).
- Update each record’s StageName.
- Increment the counter until all records are processed.
4. Update Records: Perform a bulk update.
Example 2: Validate and Process Case Comments
Scenario: Check if case comments contain a keyword and flag them for review.
Steps:
1. Get Records: Fetch all CaseComment records.
2. Loop Through Each Comment:
- Use a decision element to check if the comment contains a restricted word.
- If yes, mark it for review.
3. Update Records: Bulk update flagged comments.
Example 3: Dynamic Screen Flow for Multi-Record Selection
Scenario: Let users select multiple contacts to send an email.
Steps:
1. Get Records: Retrieve Contacts.
2. Loop Element in Screen Flow:
- Display each contact in a checkbox list.
- Let users select multiple records.
3. Process Selected Records: Send emails in bulk.
4. Common Pitfalls and How to Avoid Them
A. Infinite Loops
Issue: The flow runs indefinitely because the exit condition is missing.
Fix: Always include a counter and a maximum loop limit.
B. CPU Timeouts
Issue: Flow exceeds Salesforce governor limits (e.g., 10,000 ms CPU time).
Fix:
- Process records in batches (e.g., 200 at a time).
- Use pause elements for long-running flows.
C. Inefficient Queries
Issue: Querying too many records slows down the flow.
Fix:
- Add filters (e.g., WHERE IsActive = true).
- Use indexed fields in queries.
D. Hardcoding Limits
Issue: Assuming a fixed number of records.
Fix: Use dynamic collection sizes ({!myCollection.size}).
My Takeaway: Salesforce Flow Loops
Salesforce Flow loops are a powerful way to automate repetitive tasks, but they must be used carefully to avoid performance issues. By following best practices such as optimizing queries, preventing infinite loops, and bulkifying operations you can build efficient and scalable flows.