0

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!

2

I find it helpful to think about all the objects, data structure, and where the data you know is located. In this case, you know that DuplicateRecordItem contains the recordId (account) and the duplicateRecordSetId which is what you want.

In terms of the trigger, I'd focus on the Account as the merge action is starting there.

With this information in mind, you can essentially query the DuplicateRecordItems based on the accountIds in the trigger, and then query the DuplicateRecordSet based on the Id on the DuplicateReocrdItems.

List<DuplicateRecordItem> dupRecs = [SELECT Id,RecordId,DuplicateRecordSetId FROM DuplicateRecordItem WHERE RecordId in: accountList];

Set<Id> dupRecSet = new Set<Id>();
for(DuplicateRecordItem dupItem : dupRecs){
    dupRecSet.add(dupRecSet.DuplicateRecordSetId);
}

List<DuplicateRecordSet> dupRecSetList = [SELECT Id, ea_Status__c FROM DuplicateRecordSet WHERE Id in: dupRecSet];

//do logic on the records to update status
  • Hi Kris and thanks a lot for your answer! The thing is, i realized some time ago that when an account is merged, all the duplicate record items that have this AccountId as RecordId get deleted and all the Duplicate Record Sets get Updated. So the status(my custom field) will be updated for every Duplicate Record Set, whereas i need to update it only in the Duplicate Record Set that triggered the merge. I am so stuck in this.. In any case, i thank you again, really appreciate your help:) – Giorgos Galaios Oct 21 at 19:45
  • Hi Giorgos, I'm not really following your explanation. In the code example I provided, you'd only be updating the duplicate record set above that is related to the account that was merged (was deleted). Is that not meeting what you're asking? – Kris Goncalves Oct 22 at 11:20
  • Hi Kris, i will answer you with an example why i cannot solve my issue. So, let's imagine that i have 2 duplicate Record Sets... The 1st contains Accounts A,B,C,D and the 2nd one Contains Accounts A,B,F,G. User goes to the first DuplicateRecordSet and merges Account A with Account B. So, Account A is the master Record. The trigger will, unfortunately, fire for both duplicate record sets (since the 2nd Duplicate Record set has also the account that was merged. So as a result the status of Duplicate record set will be updated to both DRS whereas i only want to the one user edited. – Giorgos Galaios Oct 22 at 11:40
  • I try to somehow find a way to get the record that was triggered by the user but no way found yet... Also, the problem is that in this object i cannot override standard actions with lightning component for example – Giorgos Galaios Oct 22 at 11:46
  • If two accounts are in two different sets, shouldn't both sets be updated since they both now lose another duplicate record item? Either way, if that's some requirement I would look at adding "ALL ROWS" to the end of your query on line 3 in the trigger you posted on the question to pull deleted accounts as well. – Kris Goncalves Oct 22 at 15:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.