Understanding the Trigger.newMap and Trigger.oldMap maps

Trigger.newMap:


1.It is a map of IDs to the newest version of records, after they have been updated.

2.The keys of the map are the IDs of the updated records and the values are the updated records themselves.

3.You can use this map to access the updated values of fields in the records and perform any necessary operations based on these values.


Trigger.oldMap:

1.It is a map of IDs to the original versions of the records, before they were updated.

2.The keys of the map are the IDs of the records and the values are the original versions of the records.

3.You can use this map to compare the old and new values of fields in the records and perform any necessary operations based on these comparisons.


Both Trigger.newMap and Trigger.oldMap are available in before and after triggers, but the maps will only contain values for records that are being inserted, updated, or deleted.


It is important to understand the difference between Trigger.new and Trigger.newMap as well as Trigger.old and Trigger.oldMap. Trigger.new and Trigger.old are lists of records, whereas Trigger.newMap and Trigger.oldMap are maps of IDs to records.


Let’s consider a scenario where you want to send an email to the account owner whenever the account’s industry is changed.


Example: Write a Trigger to send an email to the account owner whenever the account’s industry is changed. 

trigger SendIndustryChangeEmail on Account (before update) {
   for (Account a : Trigger.new) {
      Account oldAccount = Trigger.oldMap.get(a.Id);
      if (a.Industry != oldAccount.Industry) {
         Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
         email.setToAddresses(new String[] {a.Owner.Email});
         email.setSubject('Industry Changed for Account: ' + a.Name);
         email.setPlainTextBody('The industry of the account has changed from ' + oldAccount.Industry +
								' to ' + a.Industry);
         Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});
      }
   }
}


In this code, the Trigger.new is an array of Account records that are being updated. The Trigger.oldMap is a map of IDs to the original versions of the records, before the update.


The code iterates through each account in the Trigger.new array and gets the original account using the Trigger.oldMap.get(a.Id) method, where a.Id is the ID of the current account.


The code then checks if the industry of the account has changed by comparing the a.Industry and oldAccount.Industry values. If the industry has changed, an email is sent to the account owner with the subject ‘Industry Changed for Account: [Account Name]’ and body ‘The industry of the account has changed from [Old Industry] to [New Industry]’.


This is just one example of how you can use the Trigger.newMap and Trigger.oldMap maps in Apex triggers. These maps can be used in various other scenarios as well, like updating fields, creating records, sending notifications, etc.

Leave a Reply

Your email address will not be published. Required fields are marked *