Experience Salesforce

Trigger.old in Salesforce | Context Variables in Apex Triggers

What You’ll Learn

  • Trigger.old

Trigger.old in Salesforce returns a list of the old version of sObject records, and is available in update and delete triggers.

Trigger.old in Salesforce Example:

trigger OpportunityTrigger on Opportunity (before update) {
    for (Opportunity oldOpp: Trigger.old) {
        for (Opportunity newOpp: Trigger.new) {
            if (oldOpp.Id == newOpp.Id && oldOpp.Amount != newOpp.Amount) {
                newOpp.Amount.addError(‘Amount cannot be changed’);
            }
        }
    }
}

Here, Trigger.old helps compare the old and new values of Opportunity records to ensure that the Amount field is not changed.

Next Topic

Need Extra Support? Our FREE study materials have got you covered.

Our expert-prepared study materials provide the answers you need. Clear your doubts and improve your skills with detailed notes from industry professionals.

cts-img
Rakshabandhan Sale