4

I need to merge the contacts on the basis of email id and owner Id. When I try to merge records, duplicate records are getting deleted but the fields which are empty in master record are not populating with deleted records if they having values in those fields.

For Example:

Contact1 --> Email='[email protected]' Phone='1234' ownerId = '123'         
Contact2 --> Email='[email protected]' ownerId = '123'

so criteria is getting matched since both records have same email id and owner id. When I try to merge using

merge contact2 contact1; 

then contact2 gets deleted and contact1 persists. So my question is should not I get value of phone in contact1 as '1234' once merge dml operation takes place.

1 Answer 1

6

As documented here the master record always supersedes field values of the other records. Practically I needed my own merge-logic and merge-UI because of the limitations of the default merge functionality (i.e. Users should be able to hand-pick which value supersedes and it should also work on other objects than Lead, Contact and Account).

Merge Considerations

When merging sObject records, consider the following rules and guidelines:

  • Only leads, contacts, and accounts can be merged. See sObjects That Don’t Support DML Operations.
  • You can pass a master record and up to two additional sObject records to a single merge method.
  • Using the Apex merge operation, field values on the master record always supersede the corresponding field values on the records to be merged. To preserve a merged record field value, simply set this field value on the master sObject before performing the merge.
  • External ID fields can’t be used with merge.

Your custom merge functionality could work conceptually like this:

  • get all field names for the object type form schema
  • loop over these field names
  • check if the master record has a blank value at that field during each iteration
  • decide which of the other record values should supersede an assign it to the master's field
3
  • Thanks Uwe, Is there any workaround for it? Commented May 24, 2017 at 19:05
  • 2
    One workaround is to manually merge (get fields via schema, loop over them, check for NULL and act as you want). But this logic is not trivial, because if you merge 3 records and one particular field is NULL on the master and the two other records has different values for this field - now which should supersede? No way to answer that generally. Maybe that's why Salesforce has it not covered in the default merge functionality.
    – Uwe Heim
    Commented May 24, 2017 at 19:09
  • 1
    What did you mean by "External ID fields can’t be used with merge".
    – Arpit
    Commented Jun 24, 2021 at 10:01

You must log in to answer this question.

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