+ Start a Discussion
Arvind Singh 68Arvind Singh 68 

What is Difference between Trigger.New And Trigger.Old

Best Answer chosen by Arvind Singh 68
Akshay_DhimanAkshay_Dhiman
Hi Arvind Singh

Trigger.New and Trigger.Old are both the context Variables which returns records in Lists.
Trigger.New => works for the NEW values that are entering either it may be Insert or Update.
Trigger.Old=> works for the OLD values that are already in the Fields, it may be to Delete or Update the records.

********************#########################***********************************
Trigger.old: Returns a list of the old versions of the sObject records.

Note that this sObject list is only available in the update and delete triggers.
 
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>

Please mark as best answer if it helps you.

Thank You

All Answers

Raj VakatiRaj Vakati
Trigger.new : Returns a list of the new versions of the sObject records. Note that this sObject list is only available in insert and update triggers, and the records can only be modified in before triggers.
 
Trigger.old : Returns a list of the old versions of the sObject records. Note that this sObject list is only available in update and delete triggers.
 
 
trigger MyTrigger on Account (before update) { List<Account> newTriggers = Trigger.new; List<Account> oldTriggers = Trigger.old; System.debug('We want to set the heap dump marker here'); } ​







https://developer.salesforce.com/forums/?id=906F00000008yMCIAY
Akshay_DhimanAkshay_Dhiman
Hi Arvind Singh

Trigger.New and Trigger.Old are both the context Variables which returns records in Lists.
Trigger.New => works for the NEW values that are entering either it may be Insert or Update.
Trigger.Old=> works for the OLD values that are already in the Fields, it may be to Delete or Update the records.

********************#########################***********************************
Trigger.old: Returns a list of the old versions of the sObject records.

Note that this sObject list is only available in the update and delete triggers.
 
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>

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