Trigger Context Variables
By the end of this lesson, you'll be able to:
- Use Trigger.new and Trigger.old to access the records involved
- Use Trigger.isInsert, Trigger.isBefore, and similar Boolean context checks
- Explain what Trigger.newMap and Trigger.oldMap provide over the plain Lists
Prerequisites: "Before and After Triggers"
Trigger.new and Trigger.old
trigger AccountTrigger on Account (before update) {
for (Account acc : Trigger.new) {
System.debug('New value: ' + acc.Industry);
}
for (Account acc : Trigger.old) {
System.debug('Old value: ' + acc.Industry);
}
}
trigger AccountTrigger on Account (before update) {— the trigger.for (Account acc : Trigger.new) {—Trigger.newis aList<Account>(Module 15) holding the records as they will be saved.System.debug('New value: ' + acc.Industry);— logs the incoming value.}— closes the loop.- (blank line)
for (Account acc : Trigger.old) {—Trigger.oldholds the records as they were before this change — available onupdateanddelete, sinceinserthas no "before" state to compare against.System.debug('Old value: ' + acc.Industry);— logs the prior value.}— closes the loop.}— closes the trigger.
Boolean context checks
trigger AccountTrigger on Account (before insert, before update) {
if (Trigger.isInsert) {
System.debug('This is an insert.');
}
if (Trigger.isUpdate) {
System.debug('This is an update.');
}
if (Trigger.isBefore) {
System.debug('This is running before the save.');
}
}
trigger AccountTrigger on Account (before insert, before update) {— a single trigger can handle multiple events at once, as declared in the parentheses.if (Trigger.isInsert) {— checks which event is currently firing.System.debug('This is an insert.');— runs only for inserts.}— closes theif.if (Trigger.isUpdate) {— a different check.System.debug('This is an update.');— runs only for updates.}— closes theif.if (Trigger.isBefore) {— checks the timing.System.debug('This is running before the save.');—Trigger.isInsert,Trigger.isUpdate,Trigger.isBefore,Trigger.isAfter, and their counterparts let the code branch (Module 6) based on exactly which event is currently firing.}— closes theif.}— closes the trigger.
Trigger.newMap and Trigger.oldMap
trigger AccountTrigger on Account (before update) {
for (Account acc : Trigger.new) {
Account oldVersion = Trigger.oldMap.get(acc.Id);
if (acc.Industry != oldVersion.Industry) {
System.debug('Industry changed from ' + oldVersion.Industry + ' to ' + acc.Industry);
}
}
}
trigger AccountTrigger on Account (before update) {— the trigger.for (Account acc : Trigger.new) {— loops over the incoming records.Account oldVersion = Trigger.oldMap.get(acc.Id);—Trigger.oldMapis exactly Module 18'sMap<Id, sObject>pattern, pre-built by Salesforce — looking up a specific record's before version byId, rather than by matching list position.if (acc.Industry != oldVersion.Industry) {— compares the new value to the specific matching old value.System.debug('Industry changed from ' + oldVersion.Industry + ' to ' + acc.Industry);— this reliability is what makes comparing "did this specific field actually change?" trustworthy.}— closes theif.}— closes the loop.}— closes the trigger.
Exercise
Write a before update trigger body that debugs each Account's Id whose Industry field changed, using Trigger.new and Trigger.oldMap.
Show hint
Compare acc.Industry to Trigger.oldMap.get(acc.Id).Industry.
Trigger Context Variables Quiz
My Notes
Log in to keep private notes on this lesson.
Questions about this lesson
No questions yet — be the first to ask.
Log in to ask a question about this lesson.
Summary
Trigger context variables are special, automatically-populated variables giving a trigger access to the records involved and exactly which event triggered it — Trigger.new, Trigger.old, and several Boolean flags.