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
Rohit kumar singh 1Rohit kumar singh 1 

Implementation of Trigger.new and Trigger.old

How to track the implementation of the trigger.new and trigger.old I am not getting a clear picture on this   . Anyone explain this example making the differences clear.  

trigger emailCheck on Employee__c (before update) 
{
    Map<Id,Employee__c> o = new Map<Id,Employee__c>();
    o = trigger.oldMap;
    for(Employee__c n : trigger.new)
    {
        Employee__c old = new Employee__c();
        old = o.get(n.Id);
        if(n.Email__c != old.Email__c)
        {
            n.Email__c.addError('Email cannot be changed');
        }
    }
}

Thanks and Regard
Rohit kumar singh
Best Answer chosen by Rohit kumar singh 1
Akhil AnilAkhil Anil
Hi Rohit,

The Trigger.New List contains the new record that is coming to the database and Trigger.Old list will hold the existing data that is already present in the database. In the above example the trigger is simply checking whether the email value that is coming into the database matches with the existing value. If it doesn't match then it will throw an error with the message "Email cannot be changed".

Hope that makes sense !

All Answers

Akhil AnilAkhil Anil
Hi Rohit,

The Trigger.New List contains the new record that is coming to the database and Trigger.Old list will hold the existing data that is already present in the database. In the above example the trigger is simply checking whether the email value that is coming into the database matches with the existing value. If it doesn't match then it will throw an error with the message "Email cannot be changed".

Hope that makes sense !
This was selected as the best answer
EldonEldon
Hi Rohit,

Like Akhil mentioned above trigger.new is a list that contains the latest values you entered for the sobject record that caused to trigger and trigger.old is a list that contains the old values before you changed that records.
Suppose in your employee record in which you are updating you had  values like abc@xyz.com as email and you changed it to qwerty@asd.com trigger.new contains employee record with email value qwerty@asd.com and trigger.old contains employee record with email value abc@xyz.com.

Refer this link for more clarifications : https://help.salesforce.com/articleView?id=000003789&language=en_US&type=1


Regards
Rohit kumar singh 1Rohit kumar singh 1
Thanks Akhil,
1 last thing
But can u explain me the itteration/control flow /meaning of "o.get(n.id)"
Thanks & Regard
Rohit kumar singh
Rohit kumar singh 1Rohit kumar singh 1
Is it refering the old map id of new employee record and storing into old employee variable type..??
Akhil AnilAkhil Anil
Hi Rohit,

o is a map here which means that it will store data in this form --> (ID of the Employee record,Employee record itself). So when you call o.get() by passing the ID of the record, it will return the assicated Employee record who has that Id will all the field values of that record. This will ensure that the variable old which is an instance of the Employee object has all the fields values for that record which includes the Email field.
EldonEldon
The list o has been initialised with old values of your records under updation as a map(ie key value pair-id as key and corresponding record as value) and when you do 'o.get(n.id)' it returns the value corresponding to that id (n.id) that is the old record value of that id.
 
Rohit kumar singh 1Rohit kumar singh 1
Thank you guyz.
Rohit kumar singh