Activity › Forums › Salesforce® Discussions › What is the use of oldMap and newMap context variables in Salesforce Triggers?
Tagged: Account ID, Account Record, Context Variables, DML Operations, Salesforce Records, Salesforce Triggers, sObject List Type, Trigger New, Trigger NewMap, Trigger Old, Trigger OldMap, Update, Variables and Methods
-
What is the use of oldMap and newMap context variables in Salesforce Triggers?
Posted by Anurag algoworks on July 30, 2018 at 11:22 AMWhat is the use of oldMap and newMap context variables in triggers and how are they different from ‘old’ and ‘new’ context variables?
Parul replied 7 years, 8 months ago 4 Members · 4 Replies -
4 Replies
-
Hello Anurag
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.new will have current details of the record getting inserted or updated, where as trigger.old will have the existing details for a record before it is updated or deleted.
Trigger.OldMap: A map of IDs to the old versions of the sObject records. Note that this map is only available in update and delete triggers.
Trigger.new: Trigger.new is a list of sobjects. If we are talking about a DML operation on account, then trigger.new is simply a list of accounts. Similarly when used in contact trigger, trigger.new becomes a list of contacts.
Trigger.newMap: Trigger.newMap is a map with key as ID of the record and value as the record itself. Just like the above explanation, in case of accounts when we say trigger.newMap we are talking about a map of key-value pairs where the key is the account ID and the value is the account record itself.
- [adinserter block='9']
-
Trigger.OldMap: A map of IDs to the old versions of the sObject records. Note that this map is only available in update and delete triggers.
Trigger.newMap: Trigger.newMap is a map with key as ID of the record and value as the record itself. Just like the above explanation, in case of accounts when we say trigger.newMap we are talking about a map of key-value pairs where the key is the account ID and the value is the account record itself.
A map of IDs to the new versions of the sObject records.Note that this map is only available in before update, after insert, and after update triggers.
oldMap
A map of IDs to the old versions of the sObject records.Note that this map is only available in update and delete triggers.Thanks
-
Hi,
Trigger.new return the ordered list whereas trigger.newmap returns a map which is unordered
In otherways
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.NewMap
A map of IDs to the new versions of the sObject records, this map is only available in before update, after insert, and after update triggers.Hope this helps.
-
Hi , Difference in oldMap and newMap
//Trigger.new: Contains all new Data
//Trigger.Old: Contains all old Data (before change)
//Trigger.newMap: Map contains new Data (Key: Id)
//Trigger.oldMap: Map contains old Data (Key: Id)
for(Opportunity opp:Trigger.New){
if(opp.Stage != Trigger.oldMap.get(opp.Id).Stage){
//Your logic
}
}
//Same way you can use Trigger.newMapI hope this will helpfull to you.
Thanks
Log In to reply.