9
Trigger.newMap 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.newMap in Salesforce returns a map of record IDs to the new version of sObject records, and is available in before update, after insert, after update, and after undelete triggers.
Trigger.newMap Example:
trigger AccountTrigger on Account (before update) { Map<Id, Account> nMap = Trigger.newMap; List<Contact> cList = [SELECT LastName FROM Contact WHERE AccountId IN: nMap.keySet()]; for (Contact c : cList) { Account a = nMap.get(c.AccountId); c.MailingCity = a.BillingState; } update cList; } |
In this example, Trigger.newMap is used to access the new Account records and update related Contact records accordingly.
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.