The Trigger.new and Trigger.old maps are important context variables that are used in Apex triggers to access the new and old versions of the records being processed. The Trigger.new map is a map of the new versions of the records being inserted, updated, or deleted, and the Trigger.old map is a map of the old versions of the records being updated or deleted.
Here’s how you can use the Trigger.new and Trigger.old maps in your Apex triggers:
Accessing values in the Trigger.new map:
Syntax:
sObject record = Trigger.newMap.get(recordId);
Example:
for (Id recordId : Trigger.newMap.keySet()) {
Account account = (Account)Trigger.newMap.get(recordId);
// Do something with the account
}
Example: Let’s say you want to update the Description field of all newly created accounts with a standard message.
You can use the following code to access the Trigger.new map:
trigger UpdateAccountDescription on Account (before insert) { for (Account a : Trigger.new) { a.Description = 'This is a new account created on ' + System.now(); } }
You can use the following code to access the Trigger.old map:
Accessing values in the Trigger.old map:
Syntax:
sObject record = Trigger.oldMap.get(recordId);
Example:
for (Id recordId : Trigger.oldMap.keySet()) {
Account account = (Account)Trigger.oldMap.get(recordId);
// Do something with the account
}
Example: Let’s say you want to store the old name of an account every time it is updated.
You can use the following code to access the Trigger.old map:
trigger StoreOldAccountName on Account (before update) { for (Account a : Trigger.new) { Account oldAccount = Trigger.oldMap.get(a.Id); a.Old_Name__c = oldAccount.Name; } }
Comparing values in the Trigger.new and Trigger.old maps:
Example:
for (Id recordId : Trigger.newMap.keySet()) {
Account newAccount = (Account)Trigger.newMap.get(recordId);
Account oldAccount = (Account)Trigger.oldMap.get(recordId);
if (newAccount.Name != oldAccount.Name) {
// Do something with the updated account name
}
}
You can use the following code to compare the values in the Trigger.new and Trigger.old maps:
By using the Trigger.new and Trigger.old maps, you can efficiently access and compare the new and old versions of the records being processed in your Apex triggers. This can be especially useful when you are processing multiple records at once.
Example: Let’s say you want to send an email to the account owner every time the account’s industry is changed.
You can use the following code to compare the values in the Trigger.new and Trigger.old maps:
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}); } } }