function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Shree KShree K 

Could someone please give me an exclusive case where Trigger.NewMap/OldMap could be the ultimate solution?

Hi All,

Could someone please give me an exclusive case where Trigger.NewMap/OldMap could be the ultimate solution when compared to using trigger.new/trigger.Old? I am here after spending lot of time reading about trigger context variables and getting familier with the difference between them but when i started using them my self in the code I have noticed things are getting done even when interchangeably used Trigger.New in place Trigger.NewMap or trigger.Old in place Trigger.OldMap.

1) if i have to compare old and new values field values of a record or its related record , wouldn't just Trigger.New/Trigger.Old be enough?

Note: I am aware that, here I am talking about a list(Trigger.New/Ol) and a Map(Trigger.NewMap/oldMap) and I can't use Trigger.NewMap in the event of before insert event.
Murali MattaMurali Matta
Hi Shree,

Suppose you have a custom object Custom_obj__c
Trigger.New means it is a List<Custom_obj__c>
and
Trigger.NewMap means it is a map<Id, Custom_obj__c>
In before insert context your Trigger.NewMap will always be null because in before context records are not submitted to the database, so the Id is not generated yet. That's why in before insert we don't use Trigger.NewMap But in After insert, Id is generated so we can use Trigger.NewMap
In case of before and after update, the Id has already been generated in the insert event. So we can use Trigger.NewMap in before and after update.


trigger.new is simply a list of the records being processed by the trigger, so if all you need to do is loop through them then you can use that.
trigger.newMap just allows you to target specific records by Id should you not need to process everything, or if you need to map other records back to these.

Let me know if you have any confusion..

Kindly mark this as solved if the reply was helpful.

Thanks,
Murali
Shree KShree K
Hi Murali,

sorry for the late response and Thanks for the quick turnaround, i have a question here, let's say If i want to validate the field values of older version and newer version records before performing an action.

For Example: If i have a requirement to make a check box field(Ignore) to "True" when an opp.stageName is updated as "Closed - Won" for the first time, I think  I can simply use tigger.New and trigger.OldMap.get(Opp.Id) to do the job, So my question is what kind of situation would make me to run and use both OldMap and NewMap to compare field values and perform an action.

Best Regards,
Shree 
Murali MattaMurali Matta
Hi Shree,

Sorry for the late response.

Please check below link.
https://developer.salesforce.com/forums/?id=9060G000000I4s1QAC

Thanks,
Murali
Murali MattaMurali Matta