4

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?

2 Answers 2

0

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.

2
  • Hi David, yes every Contact has the direct relationship to an Account. I thought it would change this to be an indirect relationship from the new master to the duplicate's Account. Just seems to drop it though.
    – Irene
    Commented Sep 5, 2017 at 21:54
  • 2
    In that case, @Irene, you will need to manually or programmatically create the AccountContactRelations following your merge. The only route I am aware of to do this automatically would be the after delete trigger.
    – David Reed
    Commented Sep 5, 2017 at 21:59
0

I wrote a method in my trigger handler AFTER DELETE (where deletedRecords is Trigger.old). Feel free to adapt this to your needs:

public static void createACRonContactMerge(List<Contact> deletedRecords){
      List<AccountContactRelation> acrToInsert = new List<AccountContactRelation>();
      for(Contact c : deletedRecords){
        if(c.MasterRecordId != null){
          AccountContactRelation acr = new AccountContactRelation();
          acr.AccountId = c.AccountId;
          acr.ContactId = c.MasterRecordId;
          acr.IsActive = true;
          acrToInsert.add(acr);
        }
      }
      insert acrToInsert;
    }

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .