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
bonny mankotiabonny mankotia 

trigger new and old value compare

Hello guys.I have this simple trigger i need to compare old and new values for this trigger and if its not match then update the phone field.Can somebody sort out this problem with code?

trigger SameobjTrg on Contact (before insert) 
{
    for(contact con:trigger.new)
    {
        con.MobilePhone = '9816175515';
      }
}
AshlekhAshlekh
Hi Bonny,

You cann't get the old value in Before Insert opperation. Becuase you are going to insert something new in system which was not present in system than how can you get the old value,.

-Thanks
Ashlekh Gera
bonny mankotiabonny mankotia
Thanks for suggestion AKG.But can u please provide me proper code for this?
Waqar Hussain SFWaqar Hussain SF
trigger SomeObjTrigger on Contact (before update) {

   for (Contact fc: Trigger.new) {
        if(Trigger.oldMap.get(fc.Id).PhoneNumber == fc.PhoneNumber') {
            //do your logic here
    }
}
bonny mankotiabonny mankotia
Thanks Vickey.
Waqar Hussain SFWaqar Hussain SF
Please don't forget to mark "Best Answere" for is the most useful solution. 
jyothsna reddy 5jyothsna reddy 5
Hi Bonny,
Before triggers are used to perform the logic on the same object .
 If you want to updateing  the records of the same object 'Before'' triggers are used.
 
trigger SameobjTrg on Contact (before update) {

   for (Contact fc: Trigger.new) {
        if(Trigger.oldMap.get(fc.Id).PhoneNumber == fc.PhoneNumber') {  
       //do your logic here for example   
       fc.Lastname=fc.Lastname+"updated";
          
    }
}


 After triggers are used to perform the logic on the related objects and these triggers are used access the fields values that are created by system (Ex: CreatedBy, LasteModifiedBy , Record Id etc..).
If your using "After " event you can get the error like this
User-added image