Sub topic 4
Governor limits that bulkification protects
This chapter assumes you know basic Apex syntax, sObjects, and simple SOQL and DML statements. It explains the limits that bulkified triggers are written to respect.
What governor limits are
Salesforce runs many customers on shared servers. To keep one customer's code from using all the resources, the Apex runtime enforces governor limits. These are hard caps on what a single transaction can do. If your code goes over a cap, the runtime throws a System.LimitException. You cannot catch this exception, and the whole transaction rolls back.
A transaction is one unit of work, such as a user saving a record or an API call inserting 200 records. All the code that runs because of that save shares one set of limits. This includes your trigger, other triggers on the same object, flows, and any classes they call.
The limits that matter for triggers
These are the per-transaction limits for synchronous Apex. Check the current Salesforce documentation, because values can change between releases.
| Resource | Synchronous limit |
|---|---|
| SOQL queries issued | 100 |
| Records returned by SOQL queries | 50,000 |
| DML statements issued | 150 |
| Records processed by DML | 10,000 |
| CPU time | 10,000 ms |
| Heap size | 6 MB |
The first and third rows cause most trigger failures. Both count statements, not records. A query that returns 1 record and a query that returns 2,000 records each use one query from the budget of 100.
How a loop breaks the limit
A trigger receives up to 200 records per invocation. If the code runs one query for each record, 200 records need 200 queries, and the limit is 100.
trigger ContactTrigger on Contact (before insert) {
for (Contact c : Trigger.new) {
// Anti-pattern: one SOQL query per record
Account a = [SELECT Id, Industry FROM Account WHERE Id = :c.AccountId];
if (a.Industry == 'Banking') {
c.Description = 'Banking contact';
}
}
}With 1 to 100 contacts, this trigger works. With 101 or more, query number 101 throws a LimitException. The code also fails earlier for a different reason. If a contact has no AccountId, the query returns no rows and assigning the result throws a QueryException. Small manual tests pass, and a data load fails. This is why bulk problems often reach production.
The same arithmetic applies to DML. A insert or update inside a loop uses one of the 150 DML statements for every pass through the loop.
Limits are shared across the transaction
Your trigger does not get its own budget. Suppose a Contact trigger uses 60 queries. A flow or another trigger fired by the same save uses 50 more. The total of 110 is over the limit, even though each piece looks safe alone. Writing triggers that use a small, fixed number of queries leaves room for everything else in the transaction.
The following diagram shows how work in a loop grows with the batch size, and how bulkified code stays constant.
The goal of bulkification
Bulkification means making the number of SOQL and DML statements independent of the number of records. Instead of asking "how many queries per record?", aim for "how many queries per transaction?" The answer should be a small constant such as 1 or 2. The next chapters show how to do this with collections.
Exercise
A trigger runs one SOQL query inside a for loop over Trigger.new. It passes when you save 50 records in the UI but fails when a data load sends 200 records. What error occurs, and at which record does it happen?
Show answer
The runtime throws a System.LimitException (too many SOQL queries: 101). It happens on the 101st record, because the loop runs one query per record and the limit is 100 queries per synchronous transaction. The exception cannot be caught, and the whole transaction rolls back.
Exercise
A trigger uses 5 SOQL queries for any batch size. Is it safe from the SOQL query limit? Explain.
Show answer
On its own, yes, because 5 is far below 100 and does not grow with the batch size. It still shares the limit with other triggers, flows, and classes in the same transaction, so the total across all of them must stay under 100.