
When to use Trigger.New vs Trigger.NewMap in Apex
When writing an Apex trigger, a developer should be familiar with when to use Trigger.New or Trigger.NewMap context in their code and also know which events accept which context.
In this article, we will explain how trigger.new differs from trigger.newmap and when to use which in your trigger. Lets first define what each is.
Trigger.New
Trigger.New is a collection of records on the Object the trigger is running on. It is similar to how a developer would use List i.e. Trigger.New on Case Object would be similar to List<Case>.
When the trigger runs on a specific object, then the record or records upon which the insert, update or delete operation occurs are added to a list which can be accessed easily by using Trigger.New.
Trigger.New can be used for all event types on a trigger.
- Before Insert
- Before Update
- After Insert
- After Update
- After Undelete
Example
trigger CaseTrigger on Case (before insert){ for(Case c: Trigger.New){ //iterate over list and do some action c.Subject = 'This case is being updated by trigger logic.'; } }
Trigger.NewMap
Trigger.NewMap extends on the Trigger.New where it contains the same records in a Map instead of a List. The map contains the ID of the record and the entire record.
It is similar to how a developer would use Map i.e. Trigger.NewMap on Case Object would be similar to Map<Id, Case>. Trigger.NewMap allows a developer to select specific records if needed.
Trigger.NewMap cannot be used in all Trigger events. It can only be used in the following:
- After Insert
- Before Update
- After Update
- After Undelete
Example
The statement will run only 1 time and fetch subject of all Cases related to the account records in the map:
trigger AccountTrigger on Account (After insert){ //use trigger.newmap to get results in a single transaction List<Case> cLst = [Select Subject from Case where AccountId in :Trigger.NewMap.keySet()]; for(Case c: cLst ){ //iterate over list and do some action c.Subject = 'This case is being updated by trigger logic.'; *other logic...... } }
When to use Trigger.New vs Trigger.NewMap
Use trigger.new when you want to iterate over a list and especially if the records haven’t committed to the database as you wouldn’t have the IDs of the records yet. However, as we noted above, trigger.new can be used in most before and after events.
In contrast, use trigger.newmap when you have a specific use case where you have a need to get a specific record based on a known ID etc, if you wish to not process all records in the list.
Conclusion
Trigger.New context can be used in most common scenarios and is the most useful expression as it can be passed straight to handler methods from the trigger without storing it in a list variable first. Similarly Trigger.NewMap can also be sent through to handler method for additional processing.
Example
Trigger Code
trigger AccountTrigger on Account (After insert){
AccountHandler aH = new AccountHandler();
aH.someMethod(Trigger.New);
aH.anotherMethod(Trigger.NewMap);
}
Handler Class
Public Class AccountHandler{ Public void someMethod(List<Account> aList){ } Public void anotherMethod(Map<Id,Account> aMap){ } }