When and why to use a Loop in Salesforce Flow
Use a Loop when you need to iterate over a collection of records or items (for example, a list of Contacts, Opportunities or custom objects) and perform logic or transformations on each item. Loops let you process items one-by-one inside the Flow canvas while keeping the operation bulk-safe by collecting changes and then performing a single DML operation.
High-level pattern
Typical pattern for using a Loop in a record-triggered or autolaunched Flow:
- Get Records (populate a collection)
- Loop over the collection
- Inside Loop: use Assignment and Decision elements to evaluate and change each item
- Assignment: add modified items to a separate collection for updates
- After Loop: Update Records (single bulk DML using the collected items)
Step-by-step example: Update a Contact field for a set of Contacts
Goal: For all Contacts related to an Account, set a checkbox Needs_Follow_Up__c to true if their LastActivityDate is more than 90 days old.
Flow elements and variables you’ll use:
collectionContacts(Record Collection) — result of Get RecordssingleContact(Record) — current item in LoopcontactsToUpdate(Record Collection) — accumulates modified contacts- Loop element that iterates over
collectionContacts - Decision element inside loop to check LastActivityDate
- Assignment element to set the field and add to
contactsToUpdate - Update Records element after the loop to update
contactsToUpdate
Pseudo-configuration
// Get Records
Get Records: contact where AccountId = {!record.Id} (store all records in collectionContacts)
// Loop
Loop: for each {!singleContact} in {!collectionContacts}
Decision: is LASTACTIVITYDATE <= TODAY() - 90 ?
True:
Assignment: set {!singleContact}.Needs_Follow_Up__c = True
Assignment: add {!singleContact} to {!contactsToUpdate}
False: (do nothing)
End Loop// After loop
Update Records: Update {!contactsToUpdate}
Best practices and tips
- Always operate on collections and perform DML once after the loop to avoid governor limits (do not update inside the loop).
- Prefer autolaunched Flow for scheduled bulk processing or record-triggered Flow when reacting to record changes.
- Initialize collection variables before the loop (Assignment to a new empty collection) to avoid null reference issues.
- Be careful with loops that may iterate thousands of records — add limits in your Get Records or use Scheduled Batch flows.
- When you need parallel processing or callouts, consider using subflows or Apex for complex/long-running work.
- Use Fault paths on Get/Update elements to log or handle errors gracefully.
Common mistakes
- Performing Update Records inside the loop — causes excessive DML and governor limit errors.
- Not initializing collections — leads to null pointer issues when adding items.
- Using a Flow-Built loop for very large datasets — better to use Batchable Apex or split into smaller scheduled batches.
Conclusion
Loops in Salesforce Flow are powerful for iterating collections and performing item-level logic. Follow the standard pattern (Get Records → Loop → Assign/Collect → Update Records) to keep Flows efficient and governor-limit friendly. For more complex scenarios, combine subflows, scheduled runs, or Apex to maintain performance and reliability.








Leave a Reply