Experience Salesforce

Trigger.newMap in Salesforce| Context Variables in Apex Triggers

What You’ll Learn

  • Trigger.newMap

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 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.

cts-img
Rakshabandhan Sale