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
vinni shreevinni shree 

merge accounts error

Hi I have  scenario - Create a checkbox field on Account – “Merge”.
Whenever that checkbox is true get the Account with same email(Consider this account as Duplicate) and assign all related contacts, cases, opportunities to the Main Account(On which checkbox is True) and deactivate user related to the contact assosciated to duplicate account and  Delete duplicate account
So first i tried starting from merging accounts only got this error 
FATAL_ERROR System.DmlException: Merge failed. First exception on row 0 with id 0015i00000SmnhvAAB; first error: INVALID_ID_FIELD, The master record id 0015i00000SmnhvAAB appears in the merge id list: []

Please help me with this 

Code

public class MergeDuplicates {
    public static void invoke(List<account> acc){
    list<account> accounts = [select id, name,Merge__c from account];
        for(account a: accounts){
            if(a.Merge__c==true)
            {  
                list<account> dupacc=[select name, id from account where Name=:a.Name];
                 Merge a dupacc;
                
            }
           
        }
    }

}

Trigger
trigger Mergetrig on Account (after insert, after update) {
    MergeDuplicates.invoke(Trigger.new);
}

Thank you
AnkaiahAnkaiah (Salesforce Developers) 
Hi Vinni,

Merge events do not fire their own trigger events. Instead, they fire delete and update events as follows:

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:
The before delete trigger fires.
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.
The after delete trigger fires.
The system does the specific updates required for the master record. Normal update triggers apply.

If this helps,Please mark it as best answer.

Thanks!!
vinni shreevinni shree

Hi Ankaiah,

I have understood what you said but unable to modify code please help me with that, I'm new to dev.

Thank you