How to compare old field value with the next field value in Salesforce?

How to compare old field value with the next field value in Salesforce?

Sample Trigger:

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’);
        }
    }
}

Output:



Leave a Reply