9
Trigger.oldMap in Salesforce| Context Variables in Apex Triggers
Chapter Topics
- Basics of Triggers
- When to use triggers
- Types of Apex Triggers in Salesforce
- Trigger Order of Execution
- Trigger Context Variables in Salesforce
- Trigger.new in Salesforce| Context Variables in Apex Triggers
- Trigger.old in Salesforce | Context Variables in Apex Triggers
- Trigger.newMap in Salesforce| Context Variables in Apex Triggers
- Trigger.oldMap in Salesforce| Context Variables in Apex Triggers
- Trigger Exceptions
- Best Practice in Triggers
- Trigger Helper Class Pattern
Trigger.oldMap in Salesforce returns a map of record IDs to the old version of sObject records, and is available in update and delete triggers.
Trigger.oldMap Example:
trigger OpportunityTrigger on Opportunity (before update) { Map<Id, Opportunity> oMap = Trigger.oldMap; for (Opportunity newOpp : Trigger.new) { Opportunity oldOpp = oMap.get(newOpp.Id); if (newOpp.Amount != oldOpp.Amount) { newOpp.Amount.addError(‘Amount cannot be changed’); } } } |
In this example, Trigger.oldMap helps compare old and new Opportunity records, ensuring that certain fields, like Amount, are not altered during the update.
Next TopicNeed more support?
Get a head start with our FREE study notes!
Learn more and get all the answers you need at zero cost. Improve your skills using our detailed notes prepared by industry experts to help you excel.