Intermediate 30 min read

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);
    }
}
  1. trigger AccountTrigger on Account (before update) { — the trigger.
  2. for (Account acc : Trigger.new) { — Trigger.new is a List<Account> (Module 15) holding the records as they will be saved.
  3. System.debug('New value: ' + acc.Industry); — logs the incoming value.
  4. } — closes the loop.
  5. (blank line)
  6. for (Account acc : Trigger.old) { — Trigger.old holds the records as they were before this change — available on update and delete, since insert has no "before" state to compare against.
  7. System.debug('Old value: ' + acc.Industry); — logs the prior value.
  8. } — closes the loop.
  9. } — 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.');
    }
}
  1. trigger AccountTrigger on Account (before insert, before update) { — a single trigger can handle multiple events at once, as declared in the parentheses.
  2. if (Trigger.isInsert) { — checks which event is currently firing.
  3. System.debug('This is an insert.'); — runs only for inserts.
  4. } — closes the if.
  5. if (Trigger.isUpdate) { — a different check.
  6. System.debug('This is an update.'); — runs only for updates.
  7. } — closes the if.
  8. if (Trigger.isBefore) { — checks the timing.
  9. 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.
  10. } — closes the if.
  11. } — 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);
        }
    }
}
  1. trigger AccountTrigger on Account (before update) { — the trigger.
  2. for (Account acc : Trigger.new) { — loops over the incoming records.
  3. Account oldVersion = Trigger.oldMap.get(acc.Id); — Trigger.oldMap is exactly Module 18's Map<Id, sObject> pattern, pre-built by Salesforce — looking up a specific record's before version by Id, rather than by matching list position.
  4. if (acc.Industry != oldVersion.Industry) { — compares the new value to the specific matching old value.
  5. System.debug('Industry changed from ' + oldVersion.Industry + ' to ' + acc.Industry); — this reliability is what makes comparing "did this specific field actually change?" trustworthy.
  6. } — closes the if.
  7. } — closes the loop.
  8. } — 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.

Salesforce: ○ Not connected Connect
APEX

Execution Results

Run your code to see the result.

Trigger Context Variables Quiz

1. Why is Trigger.oldMap.get(acc.Id) more reliable than matching by List position for comparing old vs new values?

Log in to submit the quiz and save your score.

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.