Sub topic 4
Transactions and Governor Limits That Affect Flows
This chapter assumes you can build a record-triggered flow, use loops and variables, and read basic Apex. You should also know the flow types from the previous chapter. Here you learn how a flow shares a transaction with other automation, which limits it can hit, and how to avoid the most common causes of limit errors.
What a transaction is
A transaction is a set of database operations that Salesforce treats as one unit. If any part fails with an unhandled error, the whole transaction rolls back and none of its changes are saved.
A flow does not get its own budget. It runs inside the transaction that started it. A save on a record can fire validation rules, before-save flows, Apex triggers, after-save flows, and workflow rules in the same transaction. They all draw from one shared set of limits.
A consequence follows. A flow can fail because of Apex, or Apex can fail because of a flow, since both spend the same limits.
Limits that matter for flows
Salesforce enforces governor limits so that no one transaction monopolizes shared resources. These are the ones flows reach most often in a synchronous transaction.
| Limit | Value per transaction | Flow elements that use it |
|---|---|---|
| SOQL queries | 100 | Get Records |
| Records retrieved by SOQL | 50,000 | Get Records |
| DML statements | 150 | Create, Update, Delete Records |
| Records processed by DML | 10,000 | Create, Update, Delete Records |
| CPU time | 10,000 ms | Loops, formulas, all elements |
| Executed elements at runtime | 2,000 per flow interview | Every element that runs |
Limits can change between releases, so confirm current values in the Salesforce documentation for your API version. Asynchronous paths, such as a scheduled path or an asynchronous action, run in a separate transaction with higher limits.
Each element that queries or changes data counts once per execution. A Get Records element counts as one query, whatever number of records it returns. A Create Records element that receives a collection of 200 records counts as one DML statement.
Bulkification in flows
Salesforce processes records in batches of up to 200. If a data load updates 200 records, a record-triggered flow runs for the batch, and the platform bulkifies the flow automatically. Elements outside loops run once for the whole batch.
The platform cannot fix a flow that places a data element inside a loop. The loop runs once per record, so the element runs 200 times and the transaction exceeds the limit of 100 queries or 150 DML statements.
The anti-pattern looks like this:
- A Loop element iterates over a collection of Opportunities.
- Inside the loop, Get Records fetches the related Account.
- Inside the loop, Update Records saves the Opportunity.
The fix has three parts:
- Run Get Records once, before the loop, and retrieve all related records.
- Inside the loop, use an Assignment element to change values and add each record to a collection variable.
- After the loop, run one Update Records element on the whole collection.
Reading a limit error
When a limit is exceeded, the transaction ends with an error such as Too many SOQL queries: 101 or Too many DML statements: 151. The flow interview fails, and everything in the transaction rolls back, including work done by other automation.
Two points help with diagnosis:
- The error names the limit, which tells you the element type to look for.
- A limit error cannot be caught by a fault path. Fault paths handle errors that an element returns, such as a validation failure. A governor limit exception ends the transaction, so the only remedy is to change the design.
Other things that consume the budget
- Several flows on the same object and trigger point share one transaction. Ten flows that each run one query use ten queries.
- A flow that updates a record can fire other automation on that record. Recursive updates multiply the cost.
- The executed-elements limit of 2,000 catches loops that run too many iterations or never end.
- Large collections held in variables consume heap memory, so filter in Get Records and retrieve only the fields you need.
Exercise
A record-triggered flow loops over 200 Cases. Inside the loop, an Update Records element saves each Case. A single-record debug run succeeds, but a data load of 200 Cases fails with "Too many DML statements: 151". Why did it fail, and how do you fix it? Can a fault path catch this error?
Show answer
The Update Records element runs once per loop iteration, so 200 records need 200 DML statements, which exceeds the limit of 150. The debug run processed one record, so it used one statement. To fix it, use an Assignment element inside the loop to change each Case and add it to a collection variable, then run one Update Records element on the collection after the loop. A fault path cannot catch this error, because a governor limit exception ends the whole transaction.