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
Jagadeesh MJagadeesh M 

Error when Merging accounts if email address change from salesforce UI using apex

Hi All,
I have trigger and handler class and trying to implement the merge operation. I'm getting the error as "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.

My 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;
}
}
}
 
AnudeepAnudeep (Salesforce Developers) 
A similar issue is discussed here. I recommend taking a look at that post to learn how to avoid this error