Sub topic 9
Bulk behavior and shared governor limits
This chapter assumes you can build a basic record-triggered flow, know what a Get Records or Create Records element does, and understand that a transaction either commits or rolls back as a whole. It explains how a flow handles many records at once, and why its limits are shared with Apex and other automation.
Why bulk behavior matters
A flow rarely runs for a single record. A data load, an API call, a list-view edit, or an Apex insert of a list can save up to 200 records in one batch. Your flow is built around one record, {!$Record}, but the platform runs it for the whole batch.
If your design only works for one record, it will break the first time someone imports a spreadsheet. The failure is usually a governor limit error that rolls back the whole transaction.
How the platform runs a flow for a batch
Salesforce processes saves in chunks of up to 200 records. For each chunk, it creates one flow interview per record that meets the entry conditions. An interview is a single running instance of a flow. Each interview has its own copy of $Record and its own variables. Interviews never see each other's variables.
The platform runs the interviews in step. When several interviews reach the same Get Records, Create Records, Update Records, or Delete Records element, it combines their work into one query or one DML statement. This is called bulkification. Because of it, a flow that saves 200 records does not run 200 queries.
Two points follow from this model:
- Only records that pass the entry conditions start an interview. Tight entry conditions reduce the work done in every batch.
- Combining happens across interviews, not across loop iterations inside one interview. This is where most bulk problems come from.
Limits are shared across the transaction
A flow does not get its own budget. Everything in the same transaction draws from the same pool of limits: Apex triggers, other record-triggered flows, workflow field updates, and anything else that runs as part of the save.
These are the synchronous per-transaction limits that matter most for flows:
| Limit | Synchronous value |
|---|---|
| SOQL queries issued | 100 |
| Records retrieved by SOQL | 50,000 |
| DML statements issued | 150 |
| Records processed by DML | 10,000 |
| CPU time | 10,000 ms |
Each Get Records element counts as a SOQL query. Each Create, Update, and Delete Records element counts as a DML statement. Before-save flows can run Get Records, so queries count there too. They cannot create, update, or delete other records, so they add no DML statements. A before-save assignment to {!$Record} is applied to the record already being saved.
A single flow can look harmless and still cause trouble in combination. Suppose an Apex trigger uses 40 queries and three flows use 30 each. Together they total 130 queries, and the transaction fails even though no single piece looks excessive.
Flows also have limits of their own, such as the number of elements executed in one interview. Check the current Salesforce documentation for the exact values, because they change between releases.
The main bulk mistake: data operations inside loops
A Loop element runs the elements in its body once per item in the collection. A Get Records, Create Records, Update Records, or Delete Records element inside that body can run once per iteration, and each run counts against the limits. A loop over 150 records with an Update Records inside can reach 150 DML statements and fail.
The safe pattern is to collect changes and write them once:
- Get the records you need before the loop, using one Get Records element.
- In the loop, use an Assignment element to change the current item.
- In a second Assignment, add the changed item to a collection variable.
- After the loop, use one Update Records element on the collection.
Flow: Account - Flag Open Opportunities (after-save)
Object: Account
Trigger: A record is created or updated
Optimize for: Actions and Related Records
Entry condition: {!$Record.Rating} = "Hot"
Run: Only when a record is updated to meet the condition requirements
Get Records (object: Opportunity)
Filter: AccountId Equals {!$Record.Id}, IsClosed Equals False
Store: all records -> {!openOpps}
Loop over {!openOpps} (current item: {!currentOpp})
Assignment
{!currentOpp.NextStep} Equals "Account is hot: follow up"
Assignment
Add {!currentOpp} to {!oppsToUpdate}
Update Records element
Records: {!oppsToUpdate}This design uses one query and one DML statement per interview. The platform can combine both across the interviews in the chunk. A flow that does the same work with an Update Records element inside the loop would multiply its DML usage by the number of open opportunities.
A second mistake is a query that depends on a loop item. Instead of a Get Records inside the loop for each item, query once with a broad filter, then match results inside the loop with a Decision or by reading fields on the items.
Shared collections and the size of the work
Bulkification reduces statement counts, but record counts still apply. The 10,000-row DML limit and the 50,000-row query limit are per transaction, not per flow. Consider an Account flow that updates every child Contact. Saving 200 Accounts with 100 Contacts each tries to update 20,000 rows and fails.
Ways to deal with this:
- Narrow the Get Records filter so it returns only the records that need to change.
- Add an entry condition so the flow runs only when the relevant field actually changed.
- Move heavy work to an asynchronous path, which runs in its own transaction with its own limits.
- Avoid retrieving all fields when you do not need them. Choose fields explicitly where the element allows it, to reduce memory use.
Asynchronous work resets the budget
An after-save flow can include a Run Asynchronously path. The work on that path runs in a separate transaction after the original save commits. That transaction gets its own counters, and the asynchronous limits are higher than the synchronous ones. For example, CPU time is 60,000 ms rather than 10,000 ms.
Asynchronous paths are not a way to ignore limits. The work still has to fit in the new transaction. Because it runs after the commit, you cannot undo the original save if the asynchronous work fails.
Entry conditions are your first bulk control
The cheapest interview is the one that never starts. Compare two flows on Opportunity:
- Flow A has no entry condition. Every update to every Opportunity starts an interview, and the flow checks the stage in a Decision element.
- Flow B uses the entry condition
{!$Record.StageName} = "Closed Won"with "Only when a record is updated to meet the condition requirements".
If a data load updates 10,000 Opportunities and 50 are Closed Won, Flow A creates 10,000 interviews and Flow B creates 50. Flow A also spends CPU time on every one of them. Put your filter in the entry condition wherever you can, and use a Decision element only for logic that depends on data fetched during the flow.
Testing bulk behavior
Test with many records, not one. A flow that passes a single-record test can still exceed limits at 200. This Apex test inserts a full chunk and checks that the before-save flow from earlier in the tutorial set NextStep on every record. It assumes that flow is active.
@IsTest
private class OpportunityFlowBulkTest {
@IsTest
static void beforeSaveFlowHandlesFullChunk() {
List<Opportunity> opps = new List<Opportunity>();
for (Integer i = 0; i < 200; i++) {
opps.add(new Opportunity(
Name = 'Bulk deal ' + i,
StageName = 'Prospecting',
CloseDate = Date.today().addDays(30),
Amount = 200000
));
}
Test.startTest();
insert opps;
Test.stopTest();
Integer updatedCount = [
SELECT COUNT() FROM Opportunity WHERE NextStep = 'Review large deal'
];
Assert.areEqual(200, updatedCount);
}
}If the test fails with a limit error such as "Too many SOQL queries: 101", look for three causes. The first is a data element inside a loop in the flow. The second is another flow or an Apex trigger on the same object that also uses queries. The third is a flow that calls an Apex action, which can run its own queries. In Flow Builder, a debug run shows which elements executed. The debug logs show how many queries each automation used.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| "Too many SOQL queries: 101" on a data load | Get Records inside a loop, or several automations adding up | Move the query before the loop, and tighten entry conditions |
| "Too many DML statements: 151" | Update or Create Records inside a loop | Collect records in a collection variable, then write once |
| "Too many DML rows: 10001" | One flow touches too many child records per chunk | Filter the query, or move the work to an asynchronous path |
| Works for one record, fails in an import | The flow was tested only with a single record | Add a 200-record test |
| Error appears only after another team's release | Shared limits consumed by new automation | Check all automation on the object |
Summary
- Records are processed in chunks of up to 200. Each qualifying record gets its own interview.
- The platform combines the same data element across interviews into one query or DML statement.
- Elements inside a loop can run once per iteration, so keep Get, Create, Update, and Delete elements outside loops.
- Limits belong to the transaction, so flows, Apex, and other automation share them.
- Entry conditions reduce the number of interviews. Asynchronous paths give heavy work its own transaction.
Exercise
An after-save flow on Case loops over 120 related Tasks and runs an Update Records element inside the loop for each Task. Describe what happens when one Case is saved, and how you would redesign the flow.
Show answer
The Update Records element can run once per iteration, so a single save can issue about 120 DML statements. That is close to the synchronous limit of 150, and any other automation in the same transaction could push it over. Redesign: use one Get Records element before the loop to fetch the Tasks, assign the changes inside the loop, add each changed Task to a collection variable, and use one Update Records element on the collection after the loop. That uses a single DML statement.
Exercise
A data load saves 10,000 Opportunities. Flow A has no entry condition and uses a Decision element to check for Closed Won. Flow B uses the entry condition {!$Record.StageName} = "Closed Won". Only 50 records are Closed Won. How many interviews does each flow create, and why does this matter for limits?
Show answer
Flow A creates an interview for every saved record, 10,000 in total, and spends CPU time and possibly queries on records it will not act on. Flow B creates 50 interviews. Interviews that do not start use no queries, DML, or CPU time. Because limits are shared by the whole transaction, a flow that avoids unneeded work leaves more room for other automation.