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
Nikhil SomvanshiNikhil Somvanshi 

Using Trigger.NewMap and Trigger.OldMap

Hello Guys,

Can anyone please give me a helpful explanation of when, how and why exactly do we use Trigger.Oldmap and Trigger.newMap.

Thanks in Advance!

Norwill GutierrezNorwill Gutierrez

Trigger.Oldmap and Trigger.newMap i use to compare the data on record after update, i mean the newMap contain al the data that you are updating, and the oldMap the data before the oupdate, so....

you can do something like 
Id = record.Id;
if(newMap.get(Id).Is_Active__c != oldMap.get(Id).Is_Active__c){
//compare the old value and new value

Thats in my case, exist many other uses.

I hope helpful.
Waqar Hussain SFWaqar Hussain SF
Trigger.OldMap: Trigger.oldMap returns map of old records which are updated with new values. These List of records already there in Database. Trigger.oldmap available in Before update, after update, Before Delete and After Delete triggers.

Trigger.NewMap: Trigger.newMap returns map of new records which are trying to insert into Database. This is available in Before Insert, Before Update, After Insert,  After Update Triggers and undelete Triggers. This list of records can only modified in Before triggers.