Sub topic 4
Transactions and governor limits that affect flows
This chapter explains the transaction a flow runs in and the limits that apply to it. Those limits cause many of the failures you will debug later. It assumes you can build a flow with variables, decisions, loops, and Get Records, Create Records, and Update Records elements. It also assumes you know the difference between before-save and after-save record-triggered flows, which the first chapter covered.
What a transaction is
A transaction is one unit of work on the Salesforce platform. Every database change inside it is either committed together or rolled back together. Salesforce also counts the resources the transaction uses, such as queries, DML statements, and CPU time, and stops it if any count passes a fixed limit. These fixed limits are called governor limits.
Three consequences matter for flows:
- A flow does not get its own budget. It shares the budget with everything else in the same transaction: Apex triggers, other flows, workflow field updates, and Apex called from the flow.
- An unhandled error rolls back the whole transaction, not only the flow. The user who saved the record sees the error, and nothing the transaction did is saved.
- A limit error is not recoverable. When a governor limit is exceeded, the transaction ends. A fault path cannot catch it and continue.
Where a flow's transaction begins and ends
Where the transaction boundaries fall depends on the flow type.
| Flow type or element | Transaction behavior |
|---|---|
| Before-save record-triggered flow | Runs inside the save transaction, before the record is written. Changes to the triggering record cost no DML statement. |
| After-save record-triggered flow, main path | Runs inside the same transaction as the save. It shares limits with triggers and other automation on that record. |
| Asynchronous path (Run Asynchronously) | Runs in a separate transaction after the original one commits. It has its own limits, which are the higher asynchronous ones. |
| Scheduled path | Runs in its own transaction at the scheduled time. |
| Screen flow | Runs until it reaches a screen. Each time the user clicks Next after a screen, the flow continues in a new transaction. |
| Pause element | Ends the transaction. The resumed interview runs in a new one. |
| Autolaunched flow | Runs in the transaction of whatever called it, such as Apex, a button, or another flow. |
In an after-save flow, an error in the main path rolls back the triggering save. An error in an asynchronous path does not undo the original save, because that transaction has already committed.
The limits that matter most for flows
These are the per-transaction limits you meet most often. Values are the standard ones. Check the current Salesforce limits documentation for your release, because numbers can change.
| Limit | Synchronous | Asynchronous |
|---|---|---|
| SOQL queries issued | 100 | 200 |
| Records returned by SOQL queries | 50,000 | 50,000 |
| DML statements issued | 150 | 150 |
| Records processed by DML | 10,000 | 10,000 |
| CPU time | 10,000 ms | 60,000 ms |
| Heap size | 6 MB | 12 MB |
| Callouts | 100 | 100 |
Flows also have a limit of their own: a single flow interview can execute at most 2,000 elements at run time. An interview is one running instance of a flow.
Here is how flow elements map to these counters:
- Each Get Records element is one SOQL query.
- Each Create Records, Update Records, or Delete Records element is one DML statement, no matter how many records it handles.
- Each Action that runs Apex uses whatever that Apex uses, and it adds to the same shared totals.
- Each Loop iteration counts its inner elements again. A loop that runs 300 times over 4 elements executes 1,200 of the 2,000 allowed.
The counters count statements, not records. One Update Records element that updates 200 records costs one DML statement. The same element inside a loop that runs 200 times costs 200.
Why queries and DML inside loops fail
This is the most common reason a flow works in testing and fails in production. With a single record, a Get Records element inside a loop runs once. With 150 records, it runs 150 times and the transaction exceeds the 100-query limit.
A record-triggered flow is bulkified automatically at the trigger level. When a data load saves 200 records at once, Salesforce groups their interviews so that a Get Records or Update Records element on the main path runs once for the whole batch. That automatic grouping does not reach inside a loop. Each iteration of a loop still runs its elements one at a time.
The pattern to avoid and the pattern to use instead:
| Avoid | Use instead |
|---|---|
| Loop over opportunities, then Get Records for each one's account | Get all needed accounts once before the loop, then look up values from that collection |
| Loop over contacts, then Update Records on each contact | Inside the loop, use an Assignment element to add the changed contact to a collection variable. After the loop, run one Update Records on the collection |
| Loop over records, then run a Create Records for each | Build a collection of new records in the loop, then create them all after the loop |
The second row is the standard fix. Inside the loop, you only change in-memory variables, which cost nothing against the limits. After the loop, one DML statement writes all the changes.
Limits are shared across automation
Because the budget is shared, a flow can fail even when it is well built. Suppose an after-save flow uses 40 queries, and an Apex trigger on the same object uses 70 on the same save. Neither is at the limit alone, but together they reach 110 and the transaction fails with Too many SOQL queries: 101.
Cascades add to this. An Update Records element in a flow can fire another record-triggered flow on a different object, and that flow can update a third. All of them run in one transaction and draw from one budget. When you diagnose a limit error, you need to find every automation that ran in that transaction, not only the flow named in the error.
Reading limit errors
The error text tells you which counter ran out. These are the ones you will see most often. Exact wording varies by release.
| Error message contains | Meaning | Usual cause in a flow |
|---|---|---|
| `Too many SOQL queries: 101` | More than 100 queries in one transaction | Get Records in a loop, or several flows and triggers adding up |
| `Too many DML statements: 151` | More than 150 DML statements | Create, Update, or Delete Records in a loop |
| `Too many DML rows: 10001` | More than 10,000 records changed | A flow writing a very large collection |
| `Apex CPU time limit exceeded` | CPU use passed 10 seconds | Heavy loops, complex formulas, or slow Apex triggers on the same save |
| `Too many query rows: 50001` | More than 50,000 records returned | A Get Records with no filter on a large object |
| A message about executed elements | The 2,000 element limit | A loop that runs many times over many elements, or a loop that never ends |
Fault paths and the transaction
A fault path is the connector you draw from an element to handle its failure. The transaction behaves differently depending on whether you add one:
- No fault path: The flow ends with an error and the transaction rolls back. In a record-triggered flow, the user's save fails.
- Fault path present: The flow follows the path, and the transaction can still commit. Changes made before the failing element stay, unless you roll them back.
That second point is a trap. If a flow creates a parent record, then fails creating a child and logs the error on the fault path, the parent record is saved without its child. The user sees no error. Design a fault path with this in mind: decide whether the partial result is acceptable, and if it is not, end the flow in a way that fails the transaction.
On the fault path, the global variable $Flow.FaultMessage holds the error text. You can use it in an email body or a log record:
An error occurred in the flow: {!$Flow.FaultMessage}
Flow: {!$Flow.CurrentDateTime}
Record Id: {!$Record.Id}$Record.Id works only in record-triggered flows. A later chapter covers fault paths in full.
Measuring limit use in a test
You can measure how much of the budget a flow uses. In an Apex test, the Limits class reports usage inside the Test.startTest() and Test.stopTest() block. This test updates 200 opportunities and reads the counters. It assumes an active record-triggered flow on Opportunity, as in the earlier examples.
@IsTest
private class FlowLimitsProbeTest {
@IsTest
static void flowStaysWithinBudget() {
Account acc = new Account(Name = 'Limits Probe Account');
insert acc;
List<Opportunity> opps = new List<Opportunity>();
for (Integer i = 0; i < 200; i++) {
opps.add(new Opportunity(
Name = 'Probe Opp ' + i,
AccountId = acc.Id,
StageName = 'Prospecting',
CloseDate = Date.today().addDays(30)
));
}
insert opps;
Test.startTest();
for (Opportunity o : opps) {
o.StageName = 'Closed Won';
}
update opps;
Integer queriesUsed = Limits.getQueries();
Integer dmlUsed = Limits.getDmlStatements();
Test.stopTest();
System.debug('SOQL queries used: ' + queriesUsed + ' of ' + Limits.getLimitQueries());
System.debug('DML statements used: ' + dmlUsed + ' of ' + Limits.getLimitDmlStatements());
Assert.isTrue(queriesUsed < 50, 'The flow should use well under half of the query limit');
}
}The threshold of 50 is an example. Pick a number that fits your own automation. If a flow keeps its query count flat as you raise the record count, it is bulk-safe. If the count grows with the record count, there is a query inside a loop.
A checklist for limit problems
- Read the exact error and note which counter failed.
- Open the debug log for that transaction and find every automation that ran. Total their usage.
- Look for elements inside loops in each flow involved.
- Replace per-record queries with one query before the loop, and per-record DML with a collection updated after the loop.
- If the work is heavy but not urgent, move it to an asynchronous path to get the higher limits and a separate transaction.
- Re-run with 200 records to confirm the fix.
Exercise
An after-save record-triggered flow on Case has a Loop over related Tasks. Inside the loop is an Update Records element that sets each Task's status. A user closes a Case with 120 Tasks and gets "Too many DML statements: 151". Why does the error happen, what change fixes it, and why can't a fault path on the Update Records element catch it?
Show answer
The Update Records element is inside the loop, so it runs once per Task. 120 iterations use 120 DML statements, and other automation on the same save, such as other flows or triggers, uses the rest of the shared budget of 150 until the limit is passed. The fix is to put an Assignment element inside the loop that changes the Task's status and adds it to a collection variable, then run one Update Records element on that collection after the loop. That uses one DML statement. A fault path cannot catch the error because a governor limit ends the whole transaction immediately, so there is no running flow left to follow the path.