+ Start a Discussion
anjisalesforce@gmail.comanjisalesforce@gmail.com 

What is the use of Trigger.OldMap and Trigger.NewMap?

Hi all,

 

What is the use of Trigger.OldMap and Trigger.NewMap?

 

Can you please give me solution

 

Thanks for your help

Alok_NagarroAlok_Nagarro

Hi,

 

Trigger.newMap - 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.

 

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.

 

for more detail follow the below link -

 

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_triggers_context_variables.htm

alok29novalok29nov

Hi Anjali,

 

Let me explain it with an example.. Let say you have a requirement where you want to save the previoues owner of an account when the account owner is changed. You understand that you need to create a  trigger on update for account. Let say you click edit button and the owner is abc. Now you need to check that if owner is changed whehter it is defferent from previuos one.Here comes the need of trigger.oldmap. You check that if the  new entered owner let say xyz is same of diffrent from the previous owner. You can do that trigger.oldmap.account.owner(abc)== trigger.newmap.account.owner (xyz)..(just an example as this is not the right way to access account owner field).

 

In the above example, you can make out that oldmap and newmap is used to compare the  old and new versions of Sobject records.

 

Thanks,

Alok

Ajay K DubediAjay K Dubedi
Hi,

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.

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks,
Ajay Dubedi
AdiiAdii
hey plz solved this trigger

Create a junction object named ‘Job Application’ between two objects named ‘Position’ and ‘Candidate’.The position and candidate has many to many relationships which will be established due to the junction object. Create a status field of type picklist on both position and candidate. If the status on a candidate record is updated to ‘Approved’, the status field on position should be updated to ‘Approved’ only if status on all related candidates have been updated to ‘Approved’.
AmanSinhaAmanSinha
Trigger.newMap - A map of IDs to the new versions of the sObject records.
Trigger.oldMap - A map of IDs to the old versions of the sObject records.
Attaching the list of event with availability of trigger context variable
User-added image
Hope this helps.


 
vivek pathak 10vivek pathak 10

I Think this Link will Be Very Usefull to Understand all the ContextVariables in triggers with scenarios.

 1.Triggers Scanarios (http://www.sfdcamplified.com/2020/10/triggerscenarios.html)