You need to sign in to do that
Don't have an account?

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
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
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
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 !
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
1 last thing
But can u explain me the itteration/control flow /meaning of "o.get(n.id)"
Thanks & Regard
Rohit kumar singh
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.
Rohit kumar singh