+ Start a Discussion

Apex Merge Statement doesn't merge the record level data

Merging two contacts:


Contact c1 = new Contact(firstname='bob', lastname='smith');
Contact c2 = new Contact(firstname='bob', lastname='smith', description='Test Description');
insert new List<Contact>{ c1, c2 };

merge c1 c2;
System.assertEquals('Test Description', c1.description);

 This code gives the following failure:

System.AssertException: Assertion Failed: Expected: Test Description, Actual: null


Is it expected behavior for the Apex merge statement to not merge the data from the deleted contacts into the master contact? 



Correct. You as a developer are expected to update the master record with the surviving values.

Got it. I figured as much, but as the documentation didn't say explicitly that it didn't do this, I was unsure if it was intentional. It would be nice if there were some built in DML options with this, like a boolean for "override blank values" or something.


Anywho, for our purposes, we're just going to route the user to the built in merge contacts page to provide the most flexibility.