

Understanding Salesforce Flow Loops
Salesforce Flow has revolutionized how admins and developers automate business processes. One of the most powerful features within Flow is the Loop element. It stands out as a critical feature for handling collections of records efficiently without writing Apex code.
This article explores what Salesforce Flow Loops are, how they work, real-life use cases with step-by-step guidance for leveraging loops to automate record processing, best practices, and common pitfalls.
What is a Salesforce Flow Loop?
A Loop in Salesforce Flow is an element that iterates over a collection variable—which is essentially a list or group of records or values—and performs actions on each individual item one at a time. Think of it as a “for each” loop in programming: the loop picks one record from the collection, performs the defined actions on it, and then moves to the next record until all records have been processed.

For example, if you have a collection of Contact records retrieved via a Get Records element, a Loop element lets you process each Contact individually inside the flow, such as updating fields, making decisions, or preparing data for bulk updates.
Why Use Loops in Salesforce Flow?
Loops enable the automation of repetitive tasks across multiple records without writing Apex code. This is especially useful when:
- You need to update multiple child records based on a parent record change.
- You want to process or evaluate each record in a collection dynamically.
- You need to build or manipulate collection variables by adding or removing records during the flow.
- You want to apply conditional logic to each record individually before bulk operations.
Without loops, performing operations on multiple records in a flow would require inefficient and static solutions.
How Does the Loop Element Work?
When you add a Loop element to your flow, you:
- Select the collection variable you want to loop through (e.g., a list of Account or Contact records).
- Choose the direction of iteration: from the first item to the last or vice versa.
- Salesforce Flow creates a loop variable that temporarily holds the current record being processed inside the loop.
Inside the loop, you can reference this current item to perform assignments, decisions, or other actions. Once the actions for the current record are completed, the loop moves on to the next record until the entire collection is processed.
Example Use Case: Bulk Updating Child Records
Imagine there is a tower building company that wants to update all Candidates (the actual location where a tower is to be constructed) under a Site when the Site’s is_Active__c checkbox is unchecked (by default, it is checked).
Instead of manually updating each Candidate or writing Apex, you can use a Record-Triggered Flow with a Loop:
- Trigger: When a Site record is updated.
- Get Records: Fetch all Candidates related to the Site.
- Loop: Iterate over each Candidate.
- Assignment: Set each Candidate’s is_Active__c field to match the Site’s value.
- Update Records: After the loop completes, update all Candidate records together in one go.
This approach ensures bulkification and avoids hitting Salesforce governor limits by performing a single update operation outside the loop.
Objects Involved:
- Site (Site__c): Represents a project site that can have multiple potential locations (candidates) within this.
- Field: Is_Active__c (Checkbox) – Indicates whether the Site is active (that is, the project is under consideration, ongoing, or in use). When the value is False, the Site is treated as inactive, meaning the project has been discontinued.
- Candidate (Candidate__c): Represents potential locations that are being considered for constructing a tower.
- Field: Do_Not_Consider__c (Checkbox) – If set to True, it means that this candidate should not be considered for further processes.
Business Requirement:
Whenever a Site is marked as inactive (Is_Active__c = False), all related Candidate records linked to that Site should be updated by setting their Do_Not_Consider__c field to True.
This ensures that candidates associated with inactive Sites are not accidentally shortlisted, processed, or considered in downstream operations.
Step-by-Step Guide to Use Loops in Flow
- Create a record-triggered flow:
- Go to the Setup > Quick Find: Flows > New Flow.
- Create a Record-triggered flow on the Site object:
Condition:
- Field: is_Active__c
- Operator: equals
- Value: false


- Get Records:
- Add the Get Records element to retrieve all Candidate records linked to the Site that has been marked as inactive.
Object: Candidate
Filter: Site that triggered the flow
How many records to store: All records
How to store: Choose fields and let Salesforce do the rest
Chosen Fields: Id, Do_Not_Consider__c



- Add a Loop Element:
- Drag the Loop element onto the canvas.
- Select the collection variable from the Get Records step.
- Choose the iteration direction: First item to last item.

- Inside the Loop:
- Add an Assignment element to update the field on the current item (the loop variable).
- Set the Do_Not_Consider__c field in the Candidate object to True:
{!Loop_through_each_candidate_record.Do_Not_Consider__c} equals true

Note: Since for this scenario, the current record’s field is to be updated, we are using the Assignment element. As per your requirement, use can also include Decision, or Action to perform operations on the current item. Use the Decision element to branch logic based on field values and Collection Variable Assignment to add updated records to a new collection for bulk update.
- After the Loop:
- Add an Update Records element to update all modified records at once.

- Activate and Test: Activate your flow and test to ensure correctness and performance.

Best Practices for Using Loops in Salesforce Flow
- Avoid DML inside the loop: Refrain from executing DML operations such as Update, Create, or Delete inside the loop. This can quickly exhaust Salesforce governor limits. Instead, collect all records to update in a collection variable and perform a single DML operation after the loop finishes.
- Filter Records Early: Apply apt filters in the Get Records element to narrow down the retrieved records early on, sot that performance is enhanced and system resource usage is minimized.
- Use Clear Naming Conventions: Name your loop variables and collection variables clearly to avoid confusion, especially in complex flows.
- Test Extensively: Test flows with varying record counts and edge cases to ensure the loop behaves as expected.
- Consider Flow Limits: Be mindful of flow limits such as CPU time and collection size. For very large datasets, consider batch processing or Apex.
Common Pitfalls to Avoid
- Performing DML in the Loop: This is the most common mistake and leads to hitting governor limits.
- Not Using Collection Variables Properly: Forgetting to add updated records to a collection variable for bulk update.
- Looping Over Empty Collections: Always check if the collection is empty before looping to avoid errors.
- Ignoring Loop Direction: Usually, the direction does not matter, but in some cases, the order of processing can affect outcomes.
Conclusion
Salesforce Flow Loops are a powerful tool to automate repetitive tasks on multiple records without writing code. By understanding how to use loops effectively—fetching collections, iterating through records, applying logic, and performing bulk updates—Salesforce professionals can build scalable, efficient automations.
In our real-life example, we saw how to update all related Candidate records when a Site becomes inactive. This type of automation not only saves time but also enforces important business rules consistently across your organization.
Following best practices such as avoiding DML inside loops and filtering records early ensures your flows run smoothly and respects Salesforce governor limits. Mastering loops in Salesforce Flow unlocks a new level of automation sophistication, enabling you to streamline complex business processes and deliver greater value with clicks, not code.