Friday, January 3, 2014

How to compare old values with new values

If you want to compare old account type and new Account type in Account object.
Step 1: Create a new custom field with the name Old Type in Account object. (API name should be Old_Type__c)

Step 2: Paste this code in Account triggers.

Trigger Code:
trigger CheckPreviousAndNewValues on Account (before update)
{
    for (Account Acc : Trigger.old)
    {
        Account OldAcc = Trigger.OldMap.get(Acc.ID);
        if(Acc.Type != OldAcc.Type)
        {
            Acc.Old_Type__c = Acc.Type;
        }
        else
        {
            Acc.Old_Type__c = OldAcc.Type;
        }
    }
}

No comments:

Post a Comment