How do I find merged Records?

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();
        }
    }
}

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

SELECT AccountId where Field = 'accountMerged'