Sub topic 5
Governor limits apply to both tools
This chapter assumes you know the Salesforce data model, can find your way around Setup, and have built a simple record-triggered flow. It also assumes you know what a loop and a collection are.
Why limits exist
Salesforce runs many customers' code on shared servers. A governor limit is a hard cap on how much one transaction can consume, so one org's automation cannot starve the others. A transaction is one unit of work that either saves all its changes or none of them.
Flow and Apex both run inside the same transaction and draw from the same limit counters. Flow is not a separate engine with its own allowance. When a flow runs, its queries and updates count against the same totals as any Apex that runs in that save.
The limits you meet most often
These are the per-transaction limits for synchronous work. Check the current Salesforce documentation before relying on exact numbers, because they can change between releases.
| Limit | Synchronous value | What it counts |
|---|---|---|
| SOQL queries | 100 | Every query, from Apex or from a Get Records element |
| Records returned by SOQL | 50,000 | Rows returned across all queries |
| DML statements | 150 | Every insert, update, or delete operation, from Apex or from a Create, Update, or Delete Records element |
| Records processed by DML | 10,000 | Rows changed across all DML |
| CPU time | 10,000 ms | Time spent running code and automation on the server |
SOQL is the Salesforce Object Query Language, used to read records. DML (Data Manipulation Language) is the set of operations that write records. In Flow, each Get Records element is a query and each Create, Update, or Delete Records element is a DML statement.
When a limit is exceeded, the platform throws an exception that cannot be caught. The whole transaction rolls back, and the user sees an error such as "Too many SOQL queries: 101".
How the same mistake looks in each tool
The classic error is doing a query or DML operation once per record, inside a loop. With one record it works. With 200 records, which is the batch size Salesforce uses when a trigger fires from a bulk load, it exceeds the limit.
In Apex, the mistake looks like this:
for (Contact c : Trigger.new) {
Account a = [SELECT Id, Name FROM Account WHERE Id = :c.AccountId];
a.Description = 'Has contacts';
update a;
}Each pass runs one query and one DML statement. At 101 contacts the query limit is exceeded.
In Flow, the same mistake is a Get Records or Update Records element placed inside a Loop element. The visual canvas hides the problem, but the counters tick up in the same way. Flow does bulkify record-triggered flows for you in some respects, but an element inside a loop still runs once per iteration.
Seeing the shared budget
The following diagram shows one save operation. Each automation spends from the same budget, and the transaction fails when any counter passes its cap.
Checking usage in Apex
Apex gives you the Limits class to read the counters. Use it while debugging to see how much a block of logic really spends.
System.debug('Queries used: ' + Limits.getQueries() + ' of ' + Limits.getLimitQueries());
System.debug('DML statements used: ' + Limits.getDmlStatements() + ' of ' + Limits.getLimitDmlStatements());
System.debug('CPU ms used: ' + Limits.getCpuTime() + ' of ' + Limits.getLimitCpuTime());For flows, open Debug Logs in Setup. The log shows the flow's elements and the cumulative limit usage reported at the end of the transaction.
Practical habits
- Keep queries and DML out of loops. In Flow, collect records in a collection variable inside the loop, then use one Update Records element after the loop.
- Query once and reuse. Fetch the records you need in a single query, then look values up from a map or collection.
- Filter early. Narrow Get Records or SOQL conditions so you do not return thousands of unneeded rows.
- Count the whole save. Ask which flows, triggers, and other automations already run on this object before adding another.
Exercise
A record-triggered flow has a Loop over 150 related records. Inside the loop is an Update Records element. A teammate says the flow is safe because it is "declarative, not code". Is that right, and how would you fix it?
Show answer
No. Flow uses the same per-transaction limits as Apex. The Update Records element inside the loop runs one DML statement per iteration, so 150 iterations exceed the 150-statement limit once any other DML in the transaction is counted, and the transaction fails. Fix it by assigning the changed values to each record inside the loop, adding the record to a collection variable, and using a single Update Records element after the loop to save the whole collection.