Looping in Salesforce Flow: How to Iterate Over Multiple Records
Feb 11, 2025
Updating many records at once in a Salesforce Flow can be game-changing for admins and developers looking to automate processes. The good news is that you can loop through a collection of records in Salesforce Flow and update each one – all in one flow run. In this article, we’ll show you exactly how to use a Salesforce Flow Loop to iterate over multiple records and update them efficiently. We’ll use a real example (updating a batch of child records) to walk through the steps. By the end, you’ll know how to handle collection variables, avoid common pitfalls, and bulk update records with ease. Let’s dive in!
Why Use a Loop in Salesforce Flow?
Loops in Salesforce Flow let you iterate over a collection of records so you can perform actions on each record. This is incredibly useful when you need to update or process multiple records as part of an automation. For instance, if an Account status changes, you might want to update all related Contacts, or if an Opportunity is closed, maybe loop through and update associated records. Instead of building separate actions for each related record (which is impossible if you don’t know the count upfront), a loop handles it dynamically.
What is a Loop element? It’s a Flow element that takes a collection variable (a list of records) and lets you handle each record one by one inside the flow. You can think of it like saying “for each record in this list, do X”. With loops, Salesforce professionals can update multiple records in Salesforce Flow without writing code, using just clicks in Flow Builder.
Example Use Case: Updating Child Records in Bulk
To make this concrete, let’s use a common scenario: automatically updating all child Contact records when an Account record is updated. Imagine your Account object has a checkbox field “Active__c” and you want every Contact under that Account to have their Active__c field match it. Instead of manually editing each contact or writing Apex code, we’ll use a flow loop to handle it.
How this will work: When an Account’s Active__c field changes, a record-triggered flow will fetch all related Contacts (that’s a collection of records). We’ll loop through that contact collection, set each Contact’s Active status to match the Account, and then update all the Contacts at once. This approach can be adapted to any scenario where you need to update many records based on some condition – the loop logic remains the same.
Step-by-Step: How to Loop Through and Update Multiple Records
Ready to build the flow? Follow these steps in Salesforce Flow Builder to configure a loop that updates multiple records:
-
Start a Record-Triggered Flow (After Save): In Setup, go to Flow and create a new Record-Triggered Flow on the Account object (since our example is triggered by an Account change). Set it to run after the record is saved so we can perform updates on related records. (If you’re using another context, you could also use an autolaunched flow or schedule-triggered flow – the loop concept will be similar.)
-
Add a Get Records element (Get Related Records): We need to gather the child records to loop through. Add a Get Records element that queries Contact records where
AccountId == $Record.Id
(the Account that triggered the flow). Make sure to allow multiple records (it should store the results in a record collection variable). This is our list of contacts to update. -
Add a Loop element: Drag a Loop element onto the canvas next. Set the collection to loop over as the collection variable from the Get Records (e.g. Get_Contacts). Choose the loop direction (it can be first to last — the order usually doesn’t matter unless you have a specific reason to reverse). The Loop element will iterate over each Contact in that collection one by one. Salesforce will create a Loop Variable (often named like Current Item from Get_Contacts) representing the single record in the collection for each iteration.
-
Inside the Loop – Assignment (update each record’s field): Now inside the loop, use an Assignment element to set the field values on the current record. In our example, we take the {!Loop_Contacts.Active__c} and assign it the value of the Account’s Active__c (which triggered the flow). Essentially:
Loop_Contacts.Active__c = True
. You can do this for any fields you need to update. Important: This doesn’t save the record yet; it just changes the value in memory for that loop record. -
Collect the records to update: Still inside the loop, we need to build a collection of all the modified records. We already have a collection from the Get, but it’s a best practice to avoid directly updating a collection you’re looping (it can cause issues). Instead, use a second Assignment element (or the same one) to add the updated record (the loop’s current record) to a “Records To Update” collection. If you don’t have a variable for this yet, create a new Record Collection Variable (e.g., updateContactCollection) of type Contact. Each iteration, add {!Loop_Contacts} to {!updateContactCollection}. This way, we accumulate a list of Contacts with the new values.
-
After the Loop – Update Records: Now we’ve exited the loop, and our updateContactCollection collection contains all the contacts with updated Active__c values. The final step is to actually save these changes to the database. Use an Update Records element, and set it to update Records in a Collection (and specify updateContactCollection). This will perform one bulk update operation on all records in that collection. Salesforce will take care of updating each Contact record in one go.
-
Save and Test the Flow: Save/activate your flow. Now test it out: change the Active__c on an Account that has multiple contacts. After saving the Account, all the related Contacts should have their Active__c field updated to match. You can also debug the flow in Flow Builder to ensure the logic runs correctly (the debug will show the loop iterating through each record).
Visual Summary of the Flow: (In your mind’s eye, or on a whiteboard, the flow looks like: Trigger (Account) → Get Contacts → Loop through Contacts → Assignment (set field on contact, add to update list) → [loop ends] → Update Records (Contacts). This pattern is the go-to solution for looping in Salesforce Flow to update multiple records.)
Best Practices and Tips for Using Loops
Loops are powerful, but you need to use them correctly to avoid hitting limits or causing slow flows. Keep these best practices in mind:
-
Avoid DML inside loops: Never place an Update Records (or Create/Delete) inside the loop itself. This would perform a database operation on each iteration, quickly running into Salesforce governor limits (which allow only 150 DML operations per transaction). Instead, always collect records in a variable and do a single update outside the loop (as we did above). This is a crucial Salesforce Flow best practice that keeps your automation scalable. (For more tips on designing efficient flows, see our article Salesforce Flow Best Practices for Efficient and Scalable Automation on this site.)
-
Limit the loop size if possible: Loops will go through each record one by one. If you expect thousands of records, consider if a Flow is the right tool or if you need to batch/bulkify further. Usually, Flows can handle a few hundred records in a loop, but large volumes might hit limits. Use entry conditions on the triggering record or the Get Records query to fetch only the records you truly need to update.
-
Use efficient queries: When using Get Records before a loop, make sure to filter precisely (e.g., only get child records that actually need updating). This keeps your collections smaller and your loop running faster.
-
Test with different scenarios: Always test your flow with various conditions – e.g., an Account with no contacts (the loop should simply do nothing), an Account with one contact, and with many contacts – to ensure your logic works in all cases.
-
Understand collection variables: A collection variable in Salesforce Flow is essentially a list (array) of items. In our case, it’s a list of records. You can build collection variables using Get Records (which returns a collection), by adding records in Assignments (as we did), or other means. Mastering collections is key to using loops effectively. (If you’re new to Flow fundamentals, you might want to read our post Salesforce Flow Types Explained for a primer on different flow types and scenarios.)
By following these tips, you’ll ensure your loop runs smoothly and your flow is bulkified and efficient.
Internal Resources and Next Steps
Mastering loops opens up a world of automation possibilities. You can apply the above pattern to countless scenarios: mass updating child records, iterating through related lists to aggregate data, or even complex logic that needs to check each record against criteria. For more advanced examples and use-cases, explore other how-to articles on our blog. For example, if you found this useful, you might also enjoy Salesforce Flow Formulas: 5 Useful Examples (Text, Date, IF), which can help if you need to incorporate formula logic in your flows.
And if you’re hungry to deepen your Salesforce Flow expertise, we’ve got something special for you.
Ready to become a Flow automation pro? Take your skills to the next level with our comprehensive course! Enroll in the 2025 Salesforce Flows Complete Guide – an online, self-paced course that covers everything from beginner basics to advanced techniques. You’ll learn best practices (like using loops effectively), real-world projects, and tips to become a Flow expert. Enroll in the Salesforce Flow course here and supercharge your Salesforce automation skills today!
Salesforce Saturdays
Join the Salesforce Saturday newsletter. Every Saturday, you'll get 1 actionable tip on Salesforce technology or career growth related to the Salesforce Industry.
We hate SPAM. We will never sell your information, for any reason.