Experience Salesforce
Trigger.newMap in Salesforce| Context Variables in Apex Triggers
What You’ll Learn
- Trigger.newMap
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 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.