Sub topic 9
Bulk Behavior and Shared Governor Limits
This chapter assumes you know the basics of Salesforce objects and fields, can build a flow in Flow Builder, and understand that a database transaction either commits fully or rolls back. It covers how record-triggered flows handle many records at once and how they share limits with other automation.
How flows run in bulk
Salesforce rarely saves one record at a time. A data load, an API call, or an Apex insert can send up to 200 records through a single save operation. A record-triggered flow does not run once per record in a separate transaction. Salesforce groups the records into a batch (a bundle of up to 200), and the flow runs against that batch.
Flow handles the grouping for you. Each record becomes its own flow interview (one running instance of the flow), but elements of the same type are bundled across interviews. For example, if 200 interviews reach the same Create Records element, Salesforce combines their work into one DML statement (a database write operation) instead of 200.
The bundling only works if your design allows it. It breaks down in one common case: a loop that contains a data element.
The main bulk mistake: data elements inside loops
Get Records, Create Records, Update Records, and Delete Records each use limits. If you place one inside a Loop element, it runs once per iteration. A loop over 150 related records with an Update Records inside it issues 150 DML statements. The limit is 150 DML statements per transaction, so the 151st would fail.
The fix is to change the record collection inside the loop and write it once, after the loop ends.
Get Records: Contacts where AccountId = {!$Record.Id} -> {!relatedContacts}
Loop over {!relatedContacts} (current item: {!contactItem})
Assignment
{!contactItem.Description} Equals "Account was updated"
{!updatedContacts} Add {!contactItem}
After the loop:
Update Records: {!updatedContacts}This version uses one query and one DML statement, no matter how many contacts exist.
Limits are shared across the transaction
Governor limits (caps Salesforce sets so one customer cannot use too many shared resources) apply to the whole transaction, not to each automation. Flows, Apex triggers, workflow field updates, and processes that run in the same save all draw from one pool.
| Limit (synchronous) | Per transaction |
|---|---|
| SOQL queries | 100 |
| DML statements | 150 |
| Records retrieved by SOQL | 50,000 |
| Records processed by DML | 10,000 |
| CPU time | 10,000 ms |
If an Apex trigger on the same object has already used 90 queries, your flow has only 10 left. A flow that works alone can fail once other automation is added.
Seeing the batch in the transaction
Common errors
- Too many SOQL queries: 101. A Get Records element inside a loop, or several flows and triggers that each query. Move the query outside the loop.
- Too many DML statements: 151. An Update or Create Records element inside a loop.
- Too many DML rows: 10001. A single save touches more records than the limit allows. Narrow the entry conditions or move the work to an asynchronous path.
- Apex CPU time limit exceeded. The total work from all automation in the transaction is too slow. Check entry conditions so the flow skips records it does not need.
A strong entry condition is the cheapest limit protection. A flow that does not start uses no limits.
Testing for bulk
A flow tested with one record can hide a loop problem. Test with a full batch in Apex.
@IsTest
private class OpportunityBulkFlowTest {
@IsTest
static void bulkInsertStaysWithinLimits() {
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 withNextStep = [
SELECT COUNT() FROM Opportunity WHERE NextStep = 'Review large deal'
];
Assert.areEqual(200, withNextStep);
}
}This test assumes the before-save flow from the earlier chapter is active. If any data element sits inside a loop, the test fails with a limit error.
Exercise
An after-save flow loops over 200 related Tasks and runs an Update Records element inside the loop for each one. What happens, and how do you fix it?
Show answer
The flow issues one DML statement per iteration, so it exceeds the 150 DML statement limit and the whole transaction rolls back. Fix it by using an Assignment element inside the loop to add each changed Task to a collection variable, then run one Update Records element on that collection after the loop.
Exercise
Your flow uses 20 SOQL queries when tested alone, but fails with "Too many SOQL queries: 101" in production. Give one likely reason.
Show answer
Limits are shared across the whole transaction. Apex triggers or other flows on the same save used the remaining queries, so the combined total passed 100.