0

I have trigger and handler class. I'm getting the error

Merge failed. First exception on row 0 with id 0012h00000UXvezAAD; first error: ENTITY_IS_DELETED, entity is deleted: []

when I try to merge the account if I change the email id on account from UI.

Trigger Code:

trigger AccountTrigger on Account (before insert, after insert, after update, before update) {
   
   if (Trigger.isAfter) {
             if (Trigger.isUpdate) {
                AccountTriggerFacade.onAfterUpdate(Trigger.new);
            }
        }
}

Apex Class Code:

 public static void onAfterUpdate(List<Account> triggerNew) {
    
         DuplicateAccountMerge(triggerNew);
         system.debug('****************DuplicateAccountMerge');
    }

private static void DuplicateAccountMerge(List<Account> triggerNew) {
         Set<String> emails = new Set<String>();
            Map<String, Account> masters = new Map<String, Account>();
            system.debug('****************DuplicateAccountMerge1');
            for(Account acc : triggernew){
                emails.add(acc.PersonEmail);
            }
            emails.remove(null);
            for(Account acc: [SELECT PersonEmail FROM Account WHERE PersonEmail = :emails AND Id NOT IN :Trigger.new]){
                masters.put(acc.PersonEmail, acc);
            }
            for(Account acc: Triggernew){
                Account master = masters.get(acc.PersonEmail);
                if(master != null) {
                    merge master acc;
                }
            }
     }

Can you some help me on this.

1 Answer 1

0

This error is because, during the merge operation, the account you are modifying is deleted.

It's not possible to get away with the error until you run your merge operation in a different execution context.

To make sure the execution context is different you can implement a Queueable class that does the merge operation. You can invoke this from the trigger context.

The other option is to mark the records duplicate and later clean via a scheduled job.

3

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .