+ Start a Discussion
finalistfinalist 

Updating contacts on Account merge()

We've created a custom order object with values that roll-up to both Account and Contact via triggers.  Neither relationship (object > Contact, object > Account) is a master/detail.

We have code in place to cover scenarios if the contact's account changes, as the contact is the primary 'owner' of these custom orders, but as these come in from the web there are frequently duplicate Accounts that get created, and when the dupes are merged the rollups are not triggered.

An update on the Contact record will recalculate the rollups, but there doesn't seem to be an effective way to cause that update; at least, not in a way that's testable.

In the Account trigger we know which accounts were merged, as the 'losing' accounts are now populated with the winning account's Id in MasterRecordId, but it seems that no matter how we go about it, the Contact update never goes through because the Account trigger completes before the Contact update occurs, and the Contact still has a reference to the (now deleted) Account.

System.DmlException: Update failed. First exception on row 0 with id <contactId>; first error: ENTITY_IS_DELETED, entity is deleted:

I've tried using @future calls and creating a scheduled batch to run 10 seconds after the Account merge occurs, but none of these appear to be working.

Has anyone figured out how to handle merge() actions like this?  Do we have to create our own merge