Sub topic 5
Run logic only when a field actually changed
This chapter assumes you can write a basic Apex trigger, use Trigger.new and Trigger.oldMap, and work with List and Map collections. It builds on the earlier chapter that explained how a trigger ends up calling itself.
Why a changed-field check helps
A re-fired trigger usually runs because the first run saved records. The second run sees those saved records, but the field your logic cares about often has not changed. If your logic reacts only to real changes in that field, the second run has nothing to do and stops.
The check compares each record in Trigger.new with its earlier version in Trigger.oldMap. Both are available in update triggers. A record is "relevant" only when the watched field differs.
Building the check
The handler method receives both the new records and the old map. It builds a list of records whose Rating changed.
public static void afterUpdate(List<Account> newList, Map<Id, Account> oldMap) {
List<Account> changed = new List<Account>();
for (Account acc : newList) {
Account oldAcc = oldMap.get(acc.Id);
if (acc.Rating != oldAcc.Rating) {
changed.add(acc);
}
}
// process only the records in 'changed'
}The trigger passes both values to the handler:
trigger AccountTrigger on Account (after update) {
if (Trigger.isAfter && Trigger.isUpdate) {
AccountTriggerHandler.afterUpdate(Trigger.new, Trigger.oldMap);
}
}Now follow a transaction. A user changes Rating from Warm to Hot. The trigger finds a change and updates Description. That update fires the trigger again. This time Rating is the same in the new and old versions, so changed is empty and no further DML happens. The loop ends after one extra run.
Common errors
- Using the check in an insert trigger.
Trigger.oldMapisnullon insert, sooldMap.get(...)throws aNullPointerException. For insert, treat every record as new, or checkTrigger.isUpdatefirst. - Watching the wrong field. If your own logic changes the same field you are watching, the next run sees another change and recurses again. Watch an input field, and write to a different output field.
- Comparing the wrong sources. The old value must come from
Trigger.oldMap, keyed by record Id. Do not query the record again to get the "old" value, because the query returns the current saved value. - Treating the check as a full guard. If two triggers or a workflow field update keep flipping the watched field back and forth, the check passes every time. Use a static guard for that case, which later chapters cover.
Checking several fields
When the logic depends on more than one field, test each one and combine the results with ||.
Boolean relevant = acc.Rating != oldAcc.Rating
|| acc.Industry != oldAcc.Industry;Keep the list short. A long chain of comparisons is hard to read and easy to get wrong, so move it into a small helper method with a clear name.
Exercise
An after update trigger on Account sets Description when Rating changes. A user edits only the Phone field. What does the changed-field check do for that record, and why is Trigger.oldMap needed?
Show answer
The check finds that Rating is the same in Trigger.new and Trigger.oldMap, so the record is not added to the changed list and no DML runs. Trigger.oldMap is needed because it holds each record's values from before the save, keyed by Id, which is the only way to compare old and new values.