Sub topic 10
Transaction context and governor limits
This chapter assumes you know basic Apex, SOQL, and DML, and that you have built a record-triggered flow in Flow Builder. It explains which transaction your invocable method runs in and which limits apply there.
One transaction, shared limits
An invocable method does not get its own transaction. It runs inside the transaction of the flow interview that called it. That transaction may also include the record save that triggered the flow, other flows, Apex triggers, and validation rules.
The governor limits are counted across the whole transaction. A method that uses 40 SOQL queries leaves fewer for everything else. Synchronous limits that matter most here:
| Limit | Synchronous value |
|---|---|
| SOQL queries issued | 100 |
| Records returned by SOQL | 50,000 |
| DML statements | 150 |
| Records processed by DML | 10,000 |
| CPU time | 10,000 ms |
| Heap size | 6 MB |
How many interviews reach your method
Flow batches interviews so that your method is called once for many records. When a record-triggered flow runs for a bulk insert of 200 records, the Apex action receives a list of up to 200 requests in a single call. That is why the method must accept a list. A SOQL query or DML statement inside a for loop would run 200 times in that call.
A screen flow differs. Each interview usually runs on its own, so the list often holds one request. The transaction ends when the flow pauses at a screen. The next transaction starts with fresh limits when the user clicks Next. Write the method for the bulk case anyway, because you cannot control how a later flow will call it.
Spend limits once per call
Query and write once for the whole list. Use a map to match results back to requests.
public with sharing class AccountOwnerLookup {
public class Request {
@InvocableVariable(label='Account Id' required=true)
public Id accountId;
}
public class Result {
@InvocableVariable(label='Owner Name')
public String ownerName;
}
@InvocableMethod(label='Get Account Owner Name')
public static List<Result> getOwnerNames(List<Request> requests) {
Set<Id> accountIds = new Set<Id>();
for (Request req : requests) {
accountIds.add(req.accountId);
}
// One query for all requests, not one per request.
Map<Id, Account> accounts = new Map<Id, Account>(
[SELECT Id, Owner.Name FROM Account WHERE Id IN :accountIds]
);
List<Result> results = new List<Result>();
for (Request req : requests) {
Result res = new Result();
Account acc = accounts.get(req.accountId);
res.ownerName = acc == null ? null : acc.Owner.Name;
results.add(res);
}
System.debug('SOQL used so far: ' + Limits.getQueries() + ' of ' + Limits.getLimitQueries());
return results;
}
}The Limits class reports consumption inside the current transaction. Limits.getQueries() returns the count used so far, including queries made by code that ran before your method. That makes it useful for debugging.
What happens when something fails
If a governor limit is exceeded, Salesforce throws a System.LimitException. You cannot catch it. The transaction is rolled back, and the flow shows an error. A limit failure is a bug to prevent through bulkification, not an error to handle.
Other unhandled exceptions also roll back the transaction. The previous chapter's pattern of Database.insert(records, false) avoids exceptions for per-record failures. It records them in each SaveResult instead, so you can return them in your results. The error-handling chapter covers fault paths in detail.
Common mistakes
- Querying inside the loop over
requests, which works in a one-record test and fails at 200. - Assuming a screen flow's single-record call means bulk is irrelevant.
- Forgetting that the triggering save, triggers, and other flows already used part of the budget.
- Testing with one record and never at 200.
Exercise
A record-triggered flow calls your invocable method after 200 accounts are inserted in one operation. Your method runs one SOQL query per request. Which limit does it exceed, and how do you fix it?
Show answer
It exceeds the 100-SOQL-query limit, and Salesforce throws an uncatchable LimitException that rolls back the transaction. Collect the IDs from all requests into a set, run one query with WHERE Id IN :ids, store the rows in a map, and look up each request's data from the map.