Skip to content
Tutorials Center
My learning

Topics

Lesson 22 of 36 ยท Automation with Flow

Flow Loops, Collections and Governor Limits

Process many records in Salesforce Flow: collection variables, Loop, Collection Filter and Sort, the no-data-element-in-a-loop rule and governor limits, with JavaScript stand-ins.

Real automation rarely handles one record. A flow might need to update every open case of an account, total the line items of an order, or flag all contacts whose email bounced. The tools for this are collections and the Loop element. Used well they are simple and safe. Used badly (a query inside a loop) they are the number one reason flows hit Salesforce's governor limits and fail in production, usually the first time someone loads a big file of records.

This lesson shows the safe patterns and the limits you must respect. As elsewhere in this chapter, the demonstrations are JavaScript stand-ins, not Salesforce.

Collections

A collection variable holds an ordered list of values or records. You create one in the Manager tab by checking Allow multiple values (collection) on a variable. Common sources of collections:

  • Get Records with All records selected returns a collection of records.
  • Loop hands you one item at a time.
  • Create Records, Update Records and Delete Records can take a whole collection and process it with one operation.

Elements for working with collections:

  • Assignment with the operator Add appends an item; Add All appends another collection; Remove, Remove All and Equals (to replace) also exist.
  • Collection Filter keeps only the items that match conditions, without a loop.
  • Collection Sort orders items by field values, ascending or descending, and can limit the count.
  • Transform maps records from one shape to another (for example opportunities to tasks), replacing many Assignments inside a loop.

Prefer the collection elements over hand-written loops when they can do the job: they are shorter and easier to review.

The Loop element

A Loop visits each item of a collection and sets a current item variable. It has two exits: For Each Item (the path to run for every item) and After Last Item (what to do afterwards). Inside the loop, the typical pattern is:

  1. Decision: is this item one we need to change?
  2. Assignment: set a field on the current item.
  3. Assignment: add the current item to a second collection (for example RecordsToUpdate).

After the loop ends (on the After Last Item path), run one Update Records element on RecordsToUpdate.

Never place Get Records, Create Records, Update Records, Delete Records or an Action that does data work inside a Loop. Each pass would run its own query or DML statement, and the limits count the total across the whole transaction.

flowchart TD
  G["Get Records: open cases for the account"] --> L{"Loop over cases"}
  L -->|"For each item"| A["Assignment: set Priority = High"]
  A --> B["Assignment: add case to CasesToUpdate"]
  B --> L
  L -->|"After last item"| U["Update Records: CasesToUpdate (one operation)"]

The safe loop pattern: change in memory, save once after the loop.

Governor limits

Salesforce is a shared (multitenant) platform, so every transaction runs under limits that stop one customer from hogging resources. The numbers that matter most for flow (for one synchronous transaction; check the current Salesforce Limits Quick Reference because values change between releases):

Limit Value (synchronous)
SOQL queries issued (each Get Records is one) 100
Records retrieved by SOQL 50,000
DML statements (each Create/Update/Delete element) 150
Records processed by DML 10,000
Executed elements in a single flow interview 2,000
CPU time 10,000 ms

A record-triggered flow shares these limits with every other flow, trigger and process that runs in the same transaction. That is why a harmless-looking flow can fail only during a data load: 200 records in one batch means 200 loop passes, and any query inside the loop would be 200 queries against a limit of 100.

When a limit is exceeded the whole transaction rolls back and the user (or load job) sees an error such as "Too many SOQL queries: 101". Nothing is partially saved, which is good for data integrity but confusing for users.

Designing for large volumes

  • Query once before the loop and reuse the collection.
  • Use a filter on the Get Records (conditions, sort, record limit) instead of loading everything and filtering in the flow.
  • For Store fields, choose Only the fields I select. Fetching fewer fields is faster and uses less memory.
  • For jobs bigger than a transaction can handle, use a Schedule-Triggered Flow (it processes records in batches) or move to Apex Batch classes.
  • Use Collection Filter and Transform to cut the number of executed elements.

Stand-in: the cost of queries inside a loop

This is a stand-in that is not Salesforce: it counts queries the way the platform does, so you can see why the bad pattern breaks at 100.

JS Stand-in: SOQL count, bad loop versus good loop

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Stand-in: filter, sort and one update

JS Stand-in: collection filter, sort and a single save

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Practical checklist

  • Is every Get/Create/Update/Delete outside loops?
  • Does a data element have a Fault path?
  • Is the entry condition tight, so the flow does not run needlessly?
  • Did you test with 200 records (a batch from the Data Loader) and not only one record in the UI? See Importing data and the Data Loader.
  • Are there other flows or triggers on the same object that share the limits?

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Where should the Update Records element go in a loop-based design?

    Collect the changed items during the loop and save them with one operation afterwards.
  2. 2. A flow works for one record but fails during a 200-record import with 'Too many SOQL queries'. Likely cause?

    Each loop pass runs another query, and the total exceeds the 100-query limit.
  3. 3. Which element filters a collection without writing a loop?

    Collection Filter keeps only matching items; Collection Sort orders them.
  4. 4. What happens to the data when a governor limit is exceeded?

    Salesforce rolls back the entire transaction, so nothing is partially saved.

Summary

  • Collections hold many records; Loop visits them one by one; Filter, Sort and Transform reduce the work.
  • Never run data elements inside a loop: query before it, save after it.
  • Governor limits (100 queries, 150 DML statements, 10,000 DML rows) apply per transaction and are shared by all automation.
  • Test bulk behavior with 200 records, not one.

Next: Flow errors, debugging and testing