Trigger.old 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.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 TopicJoin our newsletter: Get daily update on Salesforce career insights & news!
Join Now!
Need 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.