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.