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>
and
Trigger.oldMap means it is a map<Id, Custom_obj__c>
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>
and
Trigger.oldMap means it is a map<Id, Custom_obj__c>
Trigger.Old : This sObject list is only available in update and delete triggers. Trigger.oldMap: This map is only available in update and delete triggers.
which is better? using Trigger.Old or Trigger.oldMap. if suppose i am comparing between 2 values.one old and one new to check if the value is changed. mostly old map is used but i feel trigger.old can also be used.
Veena, perhaps it is too late to answer your question, but this is for others who want to know the answer. as you mentioned, people mostly use trigger.oldMap, because ther order of records in trigger.new & trigger.old might be different, so there is no gaurantee you refer to the same record while comparing (trigger.old[i] & trigger.new[i] might be different records), but if you use trigger.oldMap, you can get the old version of record using ID, hence we can gaurantee the record is same. Hope it clarifies your doubt.
Ex:- (code copied from other article) //Comparing whether Stage has been changed or not
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.
suppose you have a custom object
Custom_obj__c
and
All Answers
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.
suppose you have a custom object
Custom_obj__c
and
Trigger.Old : This sObject list is only available in update and delete triggers.
Trigger.oldMap: This map is only available in update and delete triggers.
as you mentioned, people mostly use trigger.oldMap, because ther order of records in trigger.new & trigger.old might be different, so there is no gaurantee you refer to the same record while comparing (trigger.old[i] & trigger.new[i] might be different records), but if you use trigger.oldMap, you can get the old version of record using ID, hence we can gaurantee the record is same.
Hope it clarifies your doubt.
Ex:- (code copied from other article)
//Comparing whether Stage has been changed or not
for(Opportunity opp:Trigger.New){
if(opp.Stage != Trigger.oldMap.get(opp.Id).Stage){
//Your logic
}
}
https://www.sfdc-lightning.com/2018/09/11triggers-in-salesforce-examples.html