Sub topic 3
Triggers handle up to 200 records at a time
This chapter assumes you can read basic Apex, know what a trigger is, and know the before and after events. It explains why a trigger is always written for a batch of records and not for a single record.
A trigger receives a list, not one record
You might picture a trigger running once when someone saves one Account in the UI. That is only one of the ways a trigger fires. Data Loader, the APIs, Batch Apex, and any Apex insert or update can all send many records in one operation.
Salesforce passes those records to the trigger in the context variable Trigger.new. This is a list that holds up to 200 records for each trigger invocation.
If an operation touches more than 200 records, Salesforce splits it into chunks of 200. The trigger runs once for each chunk. All the chunks still belong to the same transaction, so they share one set of governor limits.
This explains why a query inside a loop is dangerous. A trigger that runs one SOQL query per record uses 200 queries on a full chunk. A synchronous transaction allows 100 queries, so it fails. The failure can also depend on data volume. The trigger may work in the UI with one record and fail in an import.
The trigger context variables
Each variable exists only for certain events. Using one that is not available gives you null and a likely NullPointerException.
| Variable | Type | Available in |
|---|---|---|
| `Trigger.new` | List of new records | insert, update, undelete |
| `Trigger.old` | List of old records | update, delete |
| `Trigger.newMap` | Map of Id to new record | after insert, update, after undelete, and before update |
| `Trigger.oldMap` | Map of Id to old record | update, delete |
| `Trigger.size` | Integer | all events |
Two details to remember:
- In a before insert trigger the records have no Id yet, so
Trigger.newMapisnull. - You can change the records in
Trigger.newonly in before triggers.Trigger.oldandTrigger.oldMapare always read-only.
Looping over the whole batch
Always write trigger logic as a loop over the collection. Never read Trigger.new[0], because the other 199 records would be silently ignored.
To compare old and new values, look up the old record in Trigger.oldMap by Id. Do not query the database for it.
trigger OpportunityBefore on Opportunity (before update) {
System.debug('Records in this chunk: ' + Trigger.size);
for (Opportunity opp : Trigger.new) {
Opportunity oldOpp = Trigger.oldMap.get(opp.Id);
if (opp.StageName != oldOpp.StageName) {
opp.Description = 'Stage changed from '
+ oldOpp.StageName + ' to ' + opp.StageName;
}
}
}This trigger uses no SOQL and no DML. The old values come from the context variable, and the changes to opp are saved automatically because this is a before trigger. It does the same work for 1 record or 200.
Common errors
- Handling only the first record. Code such as
Trigger.new[0]works in manual testing and loses data in imports. - Assuming one chunk per transaction. Static variables and limit counters carry across the chunks of one transaction. A 450-record insert runs the trigger three times, and the queries from all three runs add up.
- Using
Trigger.newMapin before insert. The map isnullbecause the Ids do not exist yet. Loop overTrigger.newinstead. - Writing DML to the same records in a before trigger. Change the fields on
Trigger.newdirectly. An extraupdateis not needed there.
What comes next
Looping safely over Trigger.new is only the first step. The loop body must not query or run DML for each record. The next chapters show how to use Sets, Maps and Lists to do that work once for the whole batch.
Exercise
A Data Loader job inserts 500 Contacts, and a before insert trigger on Contact runs on them. How many times does the trigger run, how many records does Trigger.new hold in each run, and do the runs share governor limits?
Show answer
The trigger runs three times, with 200, 200 and 100 records in Trigger.new. The runs share one set of governor limits only if they belong to the same transaction. For an insert made through an API call such as Data Loader, the chunks are processed as part of one operation, so SOQL queries and DML statements from all runs are counted together. The key point is that the code must stay within limits for each 200-record run, and it must not waste limits across runs.