Sub topic 10
Detecting changed fields with oldMap
This chapter assumes you know basic Apex syntax, how to use Maps and Sets, and how before and after update triggers differ. It also assumes you know what Trigger.new and Trigger.old contain.
Why compare old and new values
An update trigger fires whenever a record is saved. It fires even if the field you care about was not touched. Without a check, your logic runs on every save. That wastes CPU time and can overwrite values users set on purpose.
To find out what changed, compare each record in Trigger.new with its earlier version. Trigger.oldMap holds those earlier versions. It is a Map<Id, SObject> keyed by record Id.
How oldMap works
For each record in Trigger.new, call Trigger.oldMap.get(record.Id). This returns the record as it was before the save. Compare the field on both versions.
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();
}
}
}The condition has two parts. The first part, opp.StageName != oldOpp.StageName, checks that the stage changed. The second part checks that the new value is the one you want. Together they run the logic only when a record moves into Closed Won, not on every later edit to a record that is already there.
Because this is a before trigger, setting opp.CloseDate needs no DML statement. Salesforce saves the change with the record.
Where oldMap is available
Trigger.oldMap exists only when there is a prior version of the record.
| Event | Trigger.oldMap |
|---|---|
| before insert, after insert | null |
| before update, after update | available |
| before delete, after delete | available |
| after undelete | null |
An insert has no earlier version. If one trigger handles both insert and update, calling Trigger.oldMap.get(...) during an insert throws a NullPointerException. Check the context first.
trigger OpportunityStageTrigger on Opportunity (before insert, before update) {
for (Opportunity opp : Trigger.new) {
Boolean stageChanged = Trigger.isInsert
|| opp.StageName != Trigger.oldMap.get(opp.Id).StageName;
if (stageChanged && opp.StageName == 'Closed Won') {
opp.CloseDate = Date.today();
}
}
}On insert, Trigger.isInsert is true, so || short-circuits and oldMap is never read. A new record counts as "changed" because the field went from nothing to a value.
Common errors
- Reading oldMap on insert. This causes a
NullPointerException. Guard withTrigger.isInsertorTrigger.isUpdate. - Comparing only the new value.
opp.StageName == 'Closed Won'alone is true on every later save of that record. - Calling oldMap.get inside a nested query loop. The map is already in memory. You never need SOQL to get the old values.
- Expecting changes to be visible in an after trigger. Records in
Trigger.neware read-only after the save. Detecting a change works the same way, but you cannot edit the fields.
Practice
Exercise
A trigger on Account runs on before insert and before update. It should set Description to 'Rating changed' whenever Rating differs from its previous value. Inserts should not set it. How do you avoid a NullPointerException?
Show answer
Check Trigger.isUpdate before using Trigger.oldMap. For example: if (Trigger.isUpdate && a.Rating != Trigger.oldMap.get(a.Id).Rating) { a.Description = 'Rating changed'; }. On insert Trigger.oldMap is null, so the && stops before the map is read.
Exercise
Why compare the new value with the old value instead of checking only that StageName equals 'Closed Won'?
Show answer
The trigger fires on every update. A record already in Closed Won would match on each later save and the logic would run again. Comparing with the old value limits the logic to the save where the stage actually changed.