function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
madhumadhu 

Merge triggers

What are merge triggers and when merge events occur and how to handle them?? 
KaranrajKaranraj
When you are combining(merging) records in salesforce, at the time merge operation trigger will be called. There is no specific trigger event for merge operation. Instead, they will fire delete and update events. 

Deletion of losing records
A single merge operation fires a single delete event for all records that are deleted in the merge. To determine which records were deleted as a result of a merge operation use the MasterRecordId field in Trigger.old. When a record is deleted after losing a merge operation, its MasterRecordId field is set to the ID of the winning record. The MasterRecordId field is only set in after delete trigger events. If your application requires special handling for deleted records that occur as a result of a merge, you need to use the after delete trigger event.
Update of the winning record
A single merge operation fires a single update event for the winning record only. Any child records that are reparented as a result of the merge operation do not fire triggers.

For example, if two contacts are merged, only the delete and update contact triggers fire. No triggers for records related to the contacts, such as accounts or opportunities, fire.The following is the order of events when a merge occurs:
1. The before delete trigger fires.
2. The system deletes the necessary records due to the merge, assigns new parent records to the child records, and sets the MasterRecordId field on the deleted records.
3. The after delete trigger fires.
4. The system does the specific updates required for the master record. Normal update triggers apply.

https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_triggers_merge_statements.htm

 
Jigar TrivediJigar Trivedi
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;
    }    
}
http://www.infallibletechie.com/2014/08/merge-trigger-example-in-salesforce.html