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
Siddharth ManiSiddharth Mani 

Specific use case for Trigger.old

I saw this question in multiple boards, but still haven't got a definite answer for this. I want to know use case wise why we would go for  Trigger.old specifically as I believe that Trigger.oldMap already has the old values along with Id. The simpler form of the question is why Salesforce has provided Trigger.old as a context variable.
Raj VakatiRaj Vakati

To answer your question, why do we need the map when we have list? 


Both trigger.new and old map are doing same job i.e returning the old copy of data but the real advantage will be the image you are loading bulk data you are a trigger.old contains all list of bulk records. If you wanted to compare your new record with old values in this scenario, you need to process entire list to match both your old id and new id to find the matching data which is unnecessary processing. 

But if you use trigger.oldMap you will get Id as the key so you no need to loop through all old values. Directly you can get old values by using the key.



 
Siddharth ManiSiddharth Mani
Yes - I do nuderstand what you are trying to say. I have used Trigger.oldMap in almost al scenarios. My Question is more towards what could be a specific use case for Trigger.old where Trigger.oldMap does not make sense.