Sub topic 11
Governor limits and why bulk safety matters
This chapter assumes you know the Salesforce data model, can read simple Apex and SOQL, and have seen a trigger or a record-triggered flow. It explains the limits that shape every automation choice later in this tutorial.
What governor limits are
Salesforce runs many customers on shared infrastructure. Governor limits cap what one transaction can consume, so no single customer slows the others. A transaction is one unit of work, such as a record save with all the automation it fires. If a transaction goes over a limit, Salesforce throws an uncatchable error and rolls back the whole transaction.
Common synchronous limits per transaction:
| Resource | Limit |
|---|---|
| SOQL queries | 100 |
| Records returned by SOQL | 50,000 |
| DML statements | 150 |
| Records processed by DML | 10,000 |
| CPU time | 10,000 ms |
Flow and Apex share these limits. A record-triggered flow, an Apex trigger and a managed package on the same object all draw from one budget.
Why bulk safety matters
Automation must not assume one record at a time. A data load, an API call or a list-view edit can save up to 200 records in a single batch. A trigger or flow then runs once for the whole batch.
Code that queries or updates inside a loop spends one query or DML statement per record. With 200 records, a single query in the loop uses 200 queries and breaks the limit of 100.
Unsafe and safe Apex
This trigger fails on large batches because it queries inside the loop:
trigger ContactTrigger on Contact (after insert) {
for (Contact c : Trigger.new) {
Account a = [SELECT Id, Name FROM Account WHERE Id = :c.AccountId];
a.Description = 'Has contacts';
update a;
}
}This version uses one query and one DML statement for the whole batch:
trigger ContactTrigger on Contact (after insert) {
Set<Id> accountIds = new Set<Id>();
for (Contact c : Trigger.new) {
if (c.AccountId != null) {
accountIds.add(c.AccountId);
}
}
List<Account> accounts = [SELECT Id FROM Account WHERE Id IN :accountIds];
for (Account a : accounts) {
a.Description = 'Has contacts';
}
update accounts;
}The pattern is: collect IDs, query once, change records in memory, run DML once.
Flow and the same rule
Flow Builder bulkifies most elements for you, but you can still break the limits. A Get Records, Update Records or Create Records element placed inside a loop runs once per iteration. Keep those elements outside the loop. Build a collection variable inside the loop, then update the whole collection after it.
Common errors
Too many SOQL queries: 101usually means a query inside a loop, or several automations each using part of the budget.Too many DML statements: 151means DML inside a loop.Apex CPU time limit exceededoften comes from nested loops or many automations on one object.- Tests that insert only one record hide these bugs. Test with 200 records.
How limits steer tool choice
Limits are a main reason to move heavy work out of the save transaction. Asynchronous Apex gets its own transaction with higher limits, for example 60,000 ms of CPU time. That makes it the later answer for slow or high-volume work.
Exercise
A trigger on Opportunity runs a SOQL query inside a for loop over Trigger.new. A user imports 150 opportunities and the import fails with "Too many SOQL queries: 101". Why does it fail, and how do you fix it?
Show answer
The loop runs the query once per record, so 150 records need 150 queries, above the limit of 100. Fix it by collecting the needed IDs into a Set, running one query with IN :ids before the loop, and reading the results from a Map inside the loop.