Salesforce Free Developer >

Chapter 8 - Exception Handling >

Trigger.oldMap | Context Variables in APEX Triggers

Trigger.oldMap | Context Variables in APEX Triggers

 A map of IDs to the old version of the sObject records.

trigger ApexTrigger on Opportunity (before update) {

    // Only available in Update and Delete Triggers

    Map<Id,Opportunity> oMap = new Map<Id,Opportunity>();

    oMap = trigger.oldMap;

    for(Opportunity newOpp : trigger.new)

    {

        Opportunity oldOpp = new Opportunity();

        oldOpp = oMap.get(newOpp.Id);

        if(newOpp.Amount != oldOpp.Amount)

        {

            newOpp.Amount.addError(‘Amount cannot be changed’); // Trigger Exception

        }

    }

}

Note: This map is only available in update & delete triggers.

Download Study Material

Get access to exclusive study material for Salesforce Certification and ace your exams!

Our Salesforce Certification Courses

Hey there! Glad you made it through our Salesforce Developer Training for beginners. But wait! We've got some high-in-demand Salesforce courses for you to take your Salesforce skills to the next level, making you a desired professional in the Salesforce job market.

Post a Comment

Your email address will not be published. Required fields are marked *