1

Per the Database.merge documentation, when you use the Apex merge operation, all of the losing Contact's children should be reparented to the winning Contact.

Merges the specified duplicate record into the master sObject record of the same type, deleting the duplicate, and reparenting any related records. Merges only accounts, contacts, or leads.

However, while this works great for reparenting Case.ContactId and Asset.ContactId, it fails to reparent Order.BilltoContactId.

(I didn't try other OOTB lookups to Contact)

Here's proof:

Account acct = new Account(Name = 'mergeTest');
insert acct;

Contact winner = new Contact(LastName = 'winner', AccountId = acct.Id);
Contact loser = new Contact(LastName = 'loser', AccountId = acct.Id);
List<Contact> mergeContacts = new List<Contact> {winner,loser};
insert mergeContacts;

List<Order> orders = new List<Order> {
        new Order(AccountId = acct.Id, BilltoContactId = winner.Id, EffectiveDate = Date.newInstance(2040,1,1),Status='Draft'), // winner
        new Order(AccountId = acct.Id, BilltoContactId = loser.Id, EffectiveDate = Date.newInstance(2040,1,1),Status='Draft')  // loser  
};
insert orders;

merge winner loser.Id;
Order[] ordersAfterMerge = [Select Id, BilltoContactId from Order Where AccountId = :acct.Id Order by OrderNumber];
system.assertEquals(winner.Id,ordersAfterMerge[0].BillToContactId,'order w/ winning billtocontact asis');
system.assertEquals(winner.Id,ordersAfterMerge[1].BillToContactId,'order w/ merge loser billtocontactId should be swapped to merge winner');

The last assert should be true but instead fails:

System.AssertException: Assertion Failed: order w/ merge loser billtocontactId 
should be swapped to merge winner: 
Expected: 003q000000uBH0sAAG, Actual: 003q000000uBH0tAAG

UPDATE

The reason this fails is because if you look in the schema, there is no child relationship name between Contact and Order via the Order.BilltoContactId field. Same is true for the Contact -> Order via ShipToContactId field

3

So frustrating when SFDC OOTB features don't work on standard objects

This is a Known Issue and I encourage you to indicate it 'affects you'

The workaround in the KI is unpleasant if you have an existing system. Of course, you can manually (in Apex where you do the merge operation) reparent the Order BilltoContactId.

Update: this was fixed in Winter 20 Known Issue

| improve this answer | |
1

To add to this, I tested using the standard Merge Contacts feature where the source Contact had an Order where it was the BillTo and the other Contact had no Orders. I observed the same behaviour as above but in addition the Order not only didn't update to use the target Contact record, it left a dead link in place. This would certainly cause issues with reporting and with workflow. See example:

Dead link

| improve this answer | |

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.