Sub topic 1
Why Governor Limits Exist
Prerequisites
You should know basic Apex syntax, collections such as List, Set, and Map, simple SOQL and DML, and the purpose of before and after triggers.
Why governor limits exist
Salesforce runs customer code on a shared, multitenant platform. Multitenant means many organizations use the same underlying infrastructure. One transaction that consumes unlimited database, CPU, or memory resources could affect other organizations.
Governor limits are runtime limits that cap resource use per transaction. When code exceeds a limit, Salesforce stops the transaction and raises an exception. Limits encourage predictable, bulk-safe designs rather than allowing one request to monopolize shared resources.
The limits that matter most in a transaction
The exact limits depend on the execution context. Check the current Salesforce Apex Developer Guide before relying on a value. Common synchronous limits include:
| Resource | Common synchronous limit | Typical risk |
|---|---|---|
| SOQL queries | 100 statements | Querying inside a loop |
| SOQL rows | 50,000 rows | Loading too much data |
| DML statements | 150 statements | Inserting inside a loop |
| CPU time | 10,000 milliseconds | Expensive nested processing |
| Heap size | 6 MB | Holding too many records or fields |
A limit applies to the whole transaction, not to each method. Calling several classes does not reset the counters. Asynchronous contexts have different limits, but they still have limits.
Triggers handle up to 200 records at a time
A trigger receives records in collections such as Trigger.new and Trigger.old. Salesforce commonly sends up to 200 records to one trigger invocation. Therefore, code must work correctly for one record and for a full batch.
trigger ContactBeforeInsert on Contact (before insert) {
for (Contact contactRecord : Trigger.new) {
if (String.isBlank(contactRecord.LastName)) {
contactRecord.LastName = 'Unknown';
}
}
}The loop is appropriate because it changes each in-memory record. Database queries and DML statements require extra care: place them outside loops and operate on collections.
Exercise
Why can a trigger that succeeds for one Contact fail when 200 Contacts are inserted?
Show answer
The trigger uses one shared transaction budget. Per-record queries or DML can consume the SOQL or DML statement limits when the batch grows.