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
Arvind Singh 68Arvind Singh 68 

What is Difference between Trigger.oldmap And Trigger.newmap

Best Answer chosen by Arvind Singh 68
Akshay_DhimanAkshay_Dhiman
Hi Arvind

Trigger.oldMap: A map of IDs to the old versions of the sObject records. Note that this map is only available in the update and delete triggers.  
 
suppose you have a custom object Custom_obj__c

Trigger.old means it is a List<Custom_obj__c>
 
and

Trigger.oldMap means it is a map<Id, Custom_obj__c>

Trigger.NewMap: A map of IDs to the new versions of the sObject records.

Please mark as best answer if it helps you.

Thank You

All Answers

Raj VakatiRaj Vakati
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>

 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.

https://salesforce.stackexchange.com/questions/104274/difference-between-trigger-new-and-trigger-newmap
Akshay_DhimanAkshay_Dhiman
Hi Arvind

Trigger.oldMap: A map of IDs to the old versions of the sObject records. Note that this map is only available in the update and delete triggers.  
 
suppose you have a custom object Custom_obj__c

Trigger.old means it is a List<Custom_obj__c>
 
and

Trigger.oldMap means it is a map<Id, Custom_obj__c>

Trigger.NewMap: A map of IDs to the new versions of the sObject records.

Please mark as best answer if it helps you.

Thank You
This was selected as the best answer
Shubham NandwanaShubham Nandwana
Hi Arvind,
Salesforce provides Trigger.OldMap where records with the older version (last version of record committed in the database) are stored in the map with key as their Salesforce record Ids.

Trigger.OldMap = Map<Id, OldVersionOfRecord>();

I have put an example below to show that how can we use Trigger.OldMap and Trigger.New context variables to compare the field values, because the Id of the record is common in both Trigger.OldMap and Trigger. New, that’s why we can use the record Id to get older and newer version of any record from these maps.

Here in this example, trigger compares the account number field’s old value with the new value. That is, trigger checks if the account number was changed.
If the account number is changed the trigger assigns the Type field value as “prospect” else it assigns it a value as “Other“.
trigger Compare_OldandNewvalues on Account (before update) {
 
//Here we will iterate on trigger.new list, which already holds the new values of all records.
for (Account acc: Trigger.new) {
//Here we use the account id, to get the older version of record.
Account oldAccount = Trigger.oldMap.get(acc.ID);
 
//once we get the older version, we can get any field's value from older version to compare.
if(acc.AccountNumber != oldAccount.AccountNumber) {
 
//Here is some logic being performed on a condition basis.
System.debug('--*Account Number is changed*--');
System.debug('**Old Account Number :'+oldAccount.AccountNumber);
System.debug('**New Account Number :'+acc.AccountNumber);
acc.Type = 'Prospect';
}
else{
System.debug('--**Account Number has not been Updated**--');
acc.Type = 'Other';
}
}
}

Also, please note that the Trigger.oldMap is only available in the update and delete events and cannot be accessed in insert event triggers.
Please mark as best answer if it helps you.

Shubham Nandwana.
AppPerfect Corp.
salesforce@appperfect.com
408-252-4100
http://www.appperfect.com/services/salesforce/
Salesforce Development & Operations Experts