[SalesForce] Merge duplicate Contacts across different Accounts does not create AccountContactRelation

In cleaning up our database and using the new AccountContactRelation to allow a Contact to link to multiple Accounts, I need to merge all the duplicated Contacts into one Contact with AccountContactRelation records to link to the other Accounts. In doing a merge it seems to not create the ACR – I thought it would? I tried doing it via the duplicates check manually, and via apex doing a database.merge, but neither creates the relationship.

Do I have to manually create the relationships?

Best Answer

I would only expect the merged Contacts to have AccountContactRelations if the source Contacts had them. Is that the case? The merge operation doesn't do any work to maintain the semantics of your data; it just combines field values and reparents existing child records. I would not expect it to create new relationships.

This is something that could be implemented in an after delete trigger on Contact. Iterate through the Contacts in Trigger.oldMap and check for a value in the MasterRecordId field. If that field is populated, the contact is the losing record in a merge. You could then construct and insert an AccountContactRelation between the winning Contact (whose id is the MasterRecordId) and the losing Contact's Account.