8

How do I find the accounts that were merged after the merge operation? Is there any flag being set after the merge process gets completed? For instance, does any field depict that this particular account is the one of the merged accounts?

2 Answers 2

13

In the immediate aftermath (and a short time thereafter), you can find deleted records with a MasterRecordId in the Recycle Bin.

SELECT MasterRecordId FROM Account
WHERE MasterRecordId != null
ALL ROWS

You might be able to use a left inner join but I am unable to confirm currently.

SELECT Id FROM Account WHERE Id IN (/*above query*/)

Here's the description of the field from the SOAP API documentation:

If this object was deleted as the result of a merge, this field contains the ID of the record that was kept. If this object was deleted for any other reason or has not been deleted, the value is null.


In terms of how to make use of this field, it should be populated in an after delete context. Implementing a trigger handler pattern and error handling are recommended, but omitted here for brevity:

trigger Account on Account (after delete)
{
    if (trigger.isAfter)
    {
        if (trigger.isDelete)
        {
            Map<Id, Account> masterRecords = new Map<Id, Account>();
            for (Account record : trigger.old)
            {
                masterRecords.put(
                    record.MasterRecordId,
                    new Account(Id=record.MasterRecordId, Is_Merged__c=true)
                );
            }
            update masterRecords.values();
        }
    }
}
9
  • 1
    Learn something new from you every day.....
    – Eric
    Sep 18, 2016 at 21:19
  • Glad to hear it. :)
    – Adrian Larson
    Sep 18, 2016 at 21:24
  • @Adrian.I have a doubt..We wont be able to get masterrecord id if records are being deleted from recycle bin right?What should we do for that? Sep 20, 2016 at 17:10
  • @Pri That's why I said you can only rely on this strategy in the immediate aftermath and for a short time thereafter. Eventually, the records are bound to be hard-deleted, either by the system or otherwise.
    – Adrian Larson
    Sep 20, 2016 at 17:23
  • Is it possible to override merge Button of standard Page?So that I can Update a field based on that id each time after merge button is clicked?It can also be used for future reference right? Sep 20, 2016 at 17:30
2

If you are using AccountHistory, then very easy to do from AccountHistory table with out worrying about recycle bin.

SELECT AccountId where Field = 'accountMerged'

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.