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
John King20John King20 

Why use Trigger.oldMap

Hi,
Appreciate any help here. New to Apex
I have code which updates on Contacts when Account is updated. I put the code together with the assistance of some code snippets I found online and put the rest of it together based off the requirements.

I'm trying to understand the reason for the use of Trigger.New and Trigger.oldMap. They are concepts I'm not fully familiar with and want to make sure I understand them.

Thanks for any help
for(Account account : [SELECT Id, Total__c, (SELECT Id FROM Contacts) FROM Account WHERE Id IN :Trigger.new]){
        if(account.Total__c != Trigger.oldMap.get(account.id).Total__c){
            Integer numContacts = account.Contacts.size();
            Decimal shareOfTotal = account.Total__c.divide(numberOfContacts,2)

 
Best Answer chosen by John King20
AbhinavAbhinav (Salesforce Developers) 
Supoose you are updating a Account:

Let say its Name earlier its was Abc and you are updating it with Xyz.

You will have two list now Trigger.new and Trigger.old

Trigger.old will hold Name of Account as Abc and Trigger.new will hold it as Xyz before they are commited to database.

Trigger.oldMap is just a Map with Id as Key mapped with with value same as you have with Trigger.Old.

Similarly you have Trigger.newMap as well i.e a Map with ID as key as value will be the newest one.

In your code

account.Total__c != Trigger.oldMap.get(account.id).Total__c

account.Total__c. ---> will hold the new value of filed Total.

Trigger.oldMap.get(account.id).Total__c------->

Trigger.oldMap.get(account.id)  till this part it will return you Account with field having value before update is done 

Trigger.oldMap.get(account.id).Total__c---> this will give the value of Specific total

it will hold the earlier value of Total.

The pupose of putting this check 
if(account.Total__c != Trigger.oldMap.get(account.id).Total__c)  
is to make sure that block of code only execute if there is change in Total__c Value . If update is done to other field and Total__c value is same then this block of code won't execute.

I hope I made it clear.

If it helps mark it as best answer.

Thanks!

 

All Answers

AbhinavAbhinav (Salesforce Developers) 
Supoose you are updating a Account:

Let say its Name earlier its was Abc and you are updating it with Xyz.

You will have two list now Trigger.new and Trigger.old

Trigger.old will hold Name of Account as Abc and Trigger.new will hold it as Xyz before they are commited to database.

Trigger.oldMap is just a Map with Id as Key mapped with with value same as you have with Trigger.Old.

Similarly you have Trigger.newMap as well i.e a Map with ID as key as value will be the newest one.

In your code

account.Total__c != Trigger.oldMap.get(account.id).Total__c

account.Total__c. ---> will hold the new value of filed Total.

Trigger.oldMap.get(account.id).Total__c------->

Trigger.oldMap.get(account.id)  till this part it will return you Account with field having value before update is done 

Trigger.oldMap.get(account.id).Total__c---> this will give the value of Specific total

it will hold the earlier value of Total.

The pupose of putting this check 
if(account.Total__c != Trigger.oldMap.get(account.id).Total__c)  
is to make sure that block of code only execute if there is change in Total__c Value . If update is done to other field and Total__c value is same then this block of code won't execute.

I hope I made it clear.

If it helps mark it as best answer.

Thanks!

 
This was selected as the best answer
RohitJRohitJ
Hi John,

Trigger.old returns List of sObject records eg: List<Contact>
&& 
Trigger.oldMap returns a map of old version of ids eg: <Id, Contact>
It can only be used in update, delete triggers.

You may want to go through the below documentation link to understand the trigger context variables.
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_triggers_context_variables.htm

Thanks,
Rohit


 
John King20John King20
Thanks Abhinav and RohitJ. Makes more sense now