Merge trigger example in Salesforce

Merge trigger example in Salesforce

When merge event occurs, in the deleted records, MasterRecordId field will be populated with the winning record id. The winning record will be updated with the values selected.

In the below trigger, when accounts are merged, then deleted record’s Name and Website will be stored in Account Backup custom object.

Sample Trigger:


trigger AccountMergeTrigger on Account (after delete) {

    List<Account_Backup__c> listAccountBackup = new List<Account_Backup__c>();
    for(Account acct : trigger.old) {
        if(String.isNotBlank(acct.MasterRecordId)) { 
            listAccountBackup.add(new Account_Backup__c(Name = acct.Name, Website__c = acct.Website));  
        }         
    }
    if(listAccountBackup.size() > 0) {
        insert listAccountBackup;
    }    
}

Cheers!!!

Leave a Reply