Comparing the oldvalue and new value in trigger

trigger AccountTrigger on Account (before update)
{
//This event will be fired when the user updates an existing record in Account
for(Account a:Trigger.new)
{
//collection of new records(Trigger.new)
if(a.Site==Trigger.oldmap.get(a.id).Site)
{
//a.Site will bind the new value
//getting the old value(Trigger.oldmap.get(Reference variable name.Id).Field name
a.addError('value should not be the same ');
//addError used to display an error message

}
}
//Error message will be displayed only if[oldvalue==newvalue]
}

Comments