+ Start a Discussion
Mars Rover 1489Mars Rover 1489 

Trigger on merge Accounts from Duplicate Record Set

Hi all,

i have the following case related to Merge Operation:
We use Duplicate Record Set Object and Duplicate Record Items in order to manage duplicate records for Accounts. We have a custom process where if some accounts seem to be similar, a new Duplicate Record Set is created via Apex with Duplicate Record Items the suspect Accounts. Now the case is that i need when those accounts are merged to update a custom field (Status to be updated from pending to Completed) on Duplicate Record Set Object.

I tried the following:
1) Process on Duplicate Record Item but i could not cover the case of Merge event
2) Trigger on Account object but i cannot find the Id of Duplicate Record Set in order to update its status.
3) Trigger on DuplicateRecordItem where i did the following:
trigger ea_UpdateDRSAndTask2 on DuplicateRecordItem (before delete) {
    for (DuplicateRecordItem dri : Trigger.Old) {
        Account mergedAccount = [select id, name, MasterRecordId from Account where id=: dri.RecordId];
        
        if (mergedAccount.MasterRecordId != null) {
            DuplicateRecordSet drs = [select id,ea_Status__c from DuplicateRecordSet where id =:dri.DuplicateRecordSetId];
            drs.ea_Status__c = 'Complete';
            update drs;
        }
    }
}
but i was getting SOQL Error (List has no rows for assignment) on line three. I guess first Accounts are merged and then it deleted the Duplicate Record Item so it does not have a RecordId.

Do you have any suggestions? I really feel stuck with this case.
Thanks in advance!
Best Answer chosen by Mars Rover 1489
Mars Rover 1489Mars Rover 1489
I found solution by creating a new Process on Process Builder for Duplicate Record Set Objcet. When number of Duplicate Record Items get reduced, the status gets updated. Plus, i added another action to call an @invocable apex method to perform some other operations i wanted!