Sub topic 2
Reading Trigger.new, Trigger.old, and the Context Flags
This chapter assumes you know basic Apex syntax, sObjects, and SOQL and DML. It covers the Trigger context variables that tell a trigger which records it received and which event fired it. Later chapters build on them.
What the Trigger class gives you
Inside a trigger, Apex provides a static Trigger class. Its members describe the current invocation. You don't declare them. They exist only while the trigger runs.
The record collections are:
Trigger.new: a list of the new versions of the records. It is available for insert, update, and undelete.Trigger.old: a list of the old versions. It is available for update and delete.Trigger.newMap: a map of ID to new record. It is available for before update, after insert, after update, and after undelete.Trigger.oldMap: a map of ID to old record. It is available for update and delete.
Always treat these as collections. A trigger receives up to 200 records per invocation, so Trigger.new is a list even when a user saves one record.
Context flags
Boolean flags tell you which event fired. Use them when one trigger handles several events.
| Flag | True when |
|---|---|
| `Trigger.isInsert` | The event is an insert |
| `Trigger.isUpdate` | The event is an update |
| `Trigger.isDelete` | The event is a delete |
| `Trigger.isUndelete` | The event is an undelete |
| `Trigger.isBefore` | The trigger runs before the record is saved |
| `Trigger.isAfter` | The trigger runs after the record is saved |
| `Trigger.size` | Not a flag. It holds the total number of records in the invocation |
Trigger.operationType returns the same information as one enum value, such as System.TriggerOperation.BEFORE_UPDATE. It works well in a switch statement.
A worked example
This trigger logs what each event provides. The Trigger.oldMap null check matters because that map doesn't exist on insert.
trigger OpportunityContextTrigger on Opportunity (
before insert, before update, after insert, after update
) {
System.debug('Records in this batch: ' + Trigger.size);
if (Trigger.isBefore && Trigger.isInsert) {
for (Opportunity opp : Trigger.new) {
System.debug('New opportunity: ' + opp.Name);
}
}
if (Trigger.isAfter && Trigger.isUpdate) {
for (Opportunity opp : Trigger.new) {
Opportunity oldOpp = Trigger.oldMap.get(opp.Id);
System.debug(opp.Name + ': ' + oldOpp.StageName
+ ' -> ' + opp.StageName);
}
}
}The update branch pairs each new record with its old version by ID. Do not rely on Trigger.new and Trigger.old having matching positions. Look up the old record in Trigger.oldMap instead.
How the flags combine
The diagram shows which collections exist for each event.
Common errors
- Null pointer on insert.
Trigger.oldandTrigger.oldMapare null on insert. Guard withTrigger.isUpdateor a null check before reading them. - Reading the wrong collection on delete.
Trigger.newis null on delete. UseTrigger.old. - Assigning to
Trigger.newin an after trigger. The records are read-only after save. Field changes belong in before triggers. - Assuming one record. Code such as
Trigger.new[0]ignores the other 199 records.
Exercise
A trigger on Contact handles before insert and before update. Inside it, you read Trigger.oldMap.get(c.Id). Why does the insert case fail, and how would you fix it?
Show answer
On insert there are no old versions, so Trigger.oldMap is null and the call throws a null pointer exception. Guard the access with Trigger.isUpdate, or check Trigger.oldMap != null before calling get.