Sub topic 6
Which Variables Exist in Which Event
This chapter assumes you can read basic Apex, know what DML and SOQL are, and have seen the seven trigger events: before insert, before update, before delete, after insert, after update, after delete and after undelete. Here you learn which context variables hold data in each event, so you stop hitting null pointer errors.
The rule behind the table
Salesforce fills Trigger.new, Trigger.old, Trigger.newMap and Trigger.oldMap only when the data they describe exists.
- A record has no "old" version before it is inserted.
- A record has no "new" version after it is deleted.
- A record has no Id before it is inserted, so a map keyed by Id cannot exist in before insert.
These facts explain the whole table below.
Availability by event
| Event | Trigger.new | Trigger.old | Trigger.newMap | Trigger.oldMap |
|---|---|---|---|---|
| before insert | Yes (editable) | null | null | null |
| after insert | Yes (read-only) | null | Yes | null |
| before update | Yes (editable) | Yes | Yes | Yes |
| after update | Yes (read-only) | Yes | Yes | Yes |
| before delete | null | Yes | null | Yes |
| after delete | null | Yes | null | Yes |
| after undelete | Yes (read-only) | null | Yes | null |
The two before-event rows with a map worth remembering are before insert and before delete. In before insert, Trigger.newMap is null because the records have no Ids yet. In before delete, Trigger.new is null, so you must read Trigger.old.
Reading the right variable
A delete trigger that reads Trigger.new fails, because the variable is null. This is the delete guard from the topic's examples, which reads Trigger.old:
trigger AccountDeleteGuard on Account (before delete) {
for (Account a : Trigger.old) {
if (a.AnnualRevenue != null && a.AnnualRevenue > 1000000) {
a.addError('High-revenue accounts cannot be deleted.');
}
}
}An update trigger can use both collections. Trigger.oldMap gives the prior version of each record by Id:
trigger OpportunityTrigger on Opportunity (before update) {
for (Opportunity opp : Trigger.new) {
Opportunity oldOpp = Trigger.oldMap.get(opp.Id);
if (opp.StageName != oldOpp.StageName && opp.StageName == 'Closed Won') {
opp.CloseDate = Date.today();
}
}
}This only works because the trigger runs on update. If you declared the same trigger for before insert as well, Trigger.oldMap would be null and Trigger.oldMap.get(...) would throw a System.NullPointerException.
Triggers that handle several events
When one trigger covers several events, check the context before touching a variable that might be null:
trigger ContactTrigger on Contact (before insert, before update, after delete) {
if (Trigger.isDelete) {
for (Contact c : Trigger.old) {
System.debug('Deleted: ' + c.Id);
}
} else {
for (Contact c : Trigger.new) {
if (Trigger.isUpdate) {
Contact oldC = Trigger.oldMap.get(c.Id);
System.debug('Updated from ' + oldC.LastName + ' to ' + c.LastName);
}
}
}
}Common errors
- Null pointer on
Trigger.newin a delete trigger. UseTrigger.oldorTrigger.oldMap. - Null pointer on
Trigger.oldMapin an insert trigger. Guard withTrigger.isUpdatebefore reading it. - Using
Trigger.newMapin before insert. Records have no Ids yet, so the map is null. Loop overTrigger.newinstead. - Editing
Trigger.oldorTrigger.oldMap. These are always read-only. - Editing
Trigger.newin an after event. Salesforce raises an error because the records are read-only after the save.
Exercise
A trigger on Account is declared for before insert and before update. It calls Trigger.oldMap.get(a.Id) inside a loop over Trigger.new. What happens on insert, and how do you fix it?
Show answer
On insert, Trigger.oldMap is null, so the call throws a NullPointerException. Wrap the lookup in a check such as if (Trigger.isUpdate) so it runs only when an old version exists.
Exercise
Which of Trigger.new, Trigger.old, Trigger.newMap and Trigger.oldMap are non-null in an after undelete trigger?
Show answer
Trigger.new and Trigger.newMap. The restored records are available with their Ids, and there is no old version, so Trigger.old and Trigger.oldMap are null.