Sub topic 13
Designing for bulk data and governor limits
This chapter assumes you can build a basic autolaunched or schedule-triggered flow and know the Get Records, Loop, Assignment, and Update Records elements. Some Apex helps for the code example, but you can follow the rest without it.
Why limits matter more in scheduled and autolaunched flows
Salesforce is multitenant, so it caps how much work one transaction can do. These caps are called governor limits. A flow that works on five test records can fail on five thousand because it crosses a cap.
Scheduled and autolaunched flows are at higher risk than screen flows for three reasons:
- A scheduled flow selects many records at once. Nobody is watching the run.
- An autolaunched flow is called by something else, such as a record-triggered flow, Apex, or a loop in another flow. It shares the caller's limits.
- An unhandled limit error ends the transaction and rolls back its work. In unattended runs you only find out later.
The limits that matter most
These are the per-transaction limits flows hit most often. Confirm current values in the Salesforce "Execution Governors and Limits" documentation, because they change between releases.
| Limit | Synchronous | Asynchronous |
|---|---|---|
| SOQL queries (each Get Records is one) | 100 | 200 |
| DML statements (each Create, Update, or Delete Records is one) | 150 | 150 |
| Records returned by all queries | 50,000 | 50,000 |
| Records processed by DML | 10,000 | 10,000 |
| CPU time | 10 seconds | 60 seconds |
| Executed elements in one flow interview | 2,000 | 2,000 |
A flow interview is one running instance of a flow. Every element that runs counts toward the 2,000-element cap, so each trip through a loop counts again.
The limits are shared by everything in the same transaction. If an Apex trigger, a record-triggered flow, and your subflow all run together, they draw from one budget.
The core rule: no queries or DML inside loops
Each Get Records, Create Records, Update Records, or Delete Records element inside a Loop runs once per iteration. With 200 records in the loop, one Update Records element uses 200 of your 150 DML statements, and the flow fails.
The bulk-safe pattern has four steps:
- Query once, before the loop, with Get Records. Store all matching records in a collection variable.
- Loop over the collection.
- Inside the loop, change only the loop variable. Use Assignment to set fields, then add the changed record to a second collection.
- After the loop, run one Update Records element on the second collection.
The same idea applies to Decision elements. A decision inside a loop is fine, because it uses no queries or DML. Keep only in-memory work there.
Avoid hidden loops
Queries and DML can run in loops you didn't draw:
- A Subflow element inside a loop starts a new interview each iteration. If the subflow contains a Get Records element, you spend one query per iteration.
- An Action element that calls Apex inside a loop runs once per iteration unless the Apex is written to accept a list.
- A Get Records element that runs once for every record in a collection fetched earlier is a nested query pattern. Fetch all the related records in one query and match them in memory.
Prefer passing a collection into a subflow, so the subflow runs once for the whole set. Collection variables can be input variables, as the earlier chapter on input and output variables showed.
How scheduled flows batch records
A schedule-triggered flow selects its records with the object and filter conditions on the Start element. Salesforce then processes those records in batches, with up to 200 records per batch. The flow runs once per record, but Salesforce groups the interviews so that bulkified elements (such as the Update Records element) are combined. Each batch gets its own limits.
Two consequences follow:
- A large record set does not by itself hit the 50,000-query-row limit, because the work is split into batches.
- Per-record work still counts. If each record's interview runs a Get Records element, a batch of 200 records can use 200 queries unless Salesforce combines them. Keep queries and DML out of per-record paths where you can, and design for the batch, not the single record.
Narrow the selection with filters. Filtering StageName equals Prospecting, for example, is cheaper than selecting every Opportunity and testing the stage in a Decision element. Filter in the Start element, or in Get Records, never after the fact.
Calling flows from Apex in bulk
When Apex starts an autolaunched flow, the flow shares the Apex transaction's limits. Starting one interview per record in a loop repeats the same mistake, one level up. Pass the whole list instead.
Assume a flow named Close_Stale_Cases with an input collection variable accountIds of type Text, set to "Available for input". Apex can start it once for all accounts:
List<Account> accounts = [SELECT Id FROM Account WHERE Rating = 'Hot' LIMIT 200];
List<String> accountIds = new List<String>();
for (Account a : accounts) {
accountIds.add(a.Id);
}
Map<String, Object> inputs = new Map<String, Object>();
inputs.put('accountIds', accountIds);
Flow.Interview.Close_Stale_Cases interview = new Flow.Interview.Close_Stale_Cases(inputs);
interview.start();
System.debug('Queries used: ' + Limits.getQueries() + ' of ' + Limits.getLimitQueries());
System.debug('DML statements used: ' + Limits.getDmlStatements() + ' of ' + Limits.getLimitDmlStatements());The Limits class reports usage in the current transaction. Printing it after the flow call shows what the flow consumed. Run this in anonymous Apex with the debug log open to check your design.
Other design habits
- Use Get Records with "Only the first record" when you need one record. When you need several, fetch only the fields you use and let the flow store them automatically, which keeps heap usage lower.
- Do not use a Loop to count or sum when a Get Records result's size or a formula can do it. Fewer iterations mean fewer executed elements.
- Use the fewest Update Records elements you can. Two updates on the same collection cost two DML statements.
- Test with 200 or more records. A single-record test hides every bulk problem. Testing with the Debug tool and a data load is a good start.
- Watch for recursion. If your flow updates records and another automation reacts and updates them again, the combined transaction multiplies its use of the limits.
Common mistakes
- Putting Update Records inside a loop, which is the most common failure.
- Calling a subflow with queries from inside a loop.
- Selecting all records in a scheduled flow and filtering in a Decision element.
- Testing with one record and never with a batch.
- Ignoring the 2,000-element cap on loops that process large collections inside a single interview.
Exercise
A scheduled flow loops over a collection of 150 Case records. Inside the loop, an Assignment sets Status to Closed, and then an Update Records element updates the loop variable. The flow fails with "Too many DML statements: 151". What change fixes it, and why?
Show answer
Move the update out of the loop. Inside the loop, use an Assignment to set Status on the loop variable and a second Assignment to add that record to a new collection variable. After the loop ends, use one Update Records element on that collection. The flow then uses one DML statement instead of one per record, which keeps it under the 150-statement limit.
Exercise
Apex calls an autolaunched flow once for each of 300 accounts, and the flow's first element is a Get Records on Contact. Why is this risky, and how would you redesign it?
Show answer
Each call starts a new interview, and each interview runs its own query, so 300 calls use 300 queries. That exceeds the 100-query synchronous limit. Redesign the flow to take a collection of account IDs as an input variable, run a single Get Records for all matching contacts, and call it once from Apex with the full list.