9

Trigger.newMap in Salesforce| Context Variables in Apex Triggers

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 Topic

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.

ServiceNow Stripe