Trigger.oldMap example in Salesforce

Trigger.oldMap example in Salesforce

Sample Trigger:

trigger AccountTrigger on Account ( before update ) {

    for ( Account objAcc : trigger.new ) {
    
        Account objAccWithPriorValues = trigger.oldMap.get( objAcc.Id );
        
        if ( objAcc.Name != objAccWithPriorValues.Name ) {
        
            objAcc.Name.addError( 'Account Name cannot be changed' );
        
        }
    
    }

}

Output:

Leave a Reply