Dynamics CRM Merge Two Contacts with C# code, modified example from SDK

crmmergemicrosoft-dynamicsrecordssdk

I've been trying to get the Merge example in Dynamics CRM 2011 SDK to work.
http://msdn.microsoft.com/en-us/library/hh547408.aspx

I've modified it a bit. I've created two Contacts instead of Accounts (although some variable names in the code might suggest otherwise. For example _account1Id is in fact a GUID for contact1.)

The first Contact record has name, surname and telephone fields filled.
The second Contact record has name, surname and email fields filled.

The part where merge occurs is below. The original code can bee seen from the link at the top.

When I run the example with following modifications, the e-mail address doesn't get merged into the new contact record. What I get is one merged Contact with the values from one of the records, with address data added, but no e-mail. I thought this was supposed to fill empty fields of the primary record with the non-empty fields from the second record.

Being very new to Ms Dynamics CRM, I couldn't understand the reason after much googling and debugging. I'll be glad if someone can give me some feedback about what the problem might be.

Thanks in advance.

      _serviceProxy.EnableProxyTypes();
            CreateRequiredRecords(); // created two contacts with same name, surname. first record has telephone1 filled, second record has emailaddress filled.
            EntityReference target = new EntityReference();
            target.Id = _account1Id;
            target.LogicalName = Contact.EntityLogicalName;
            MergeRequest merge = new MergeRequest();
            merge.SubordinateId = _account2Id;
            merge.Target = target;
            merge.PerformParentingChecks = false;
            Contact updateContent = new Contact();
            updateContent.Address1_Line1 = "test";
            merge.UpdateContent = updateContent;
            MergeResponse merged = (MergeResponse)_serviceProxy.Execute(merge);
            Contact mergeeAccount =
                (Contact)_serviceProxy.Retrieve(Contact.EntityLogicalName,
                _account2Id, new ColumnSet(allColumns: true));
            if (mergeeAccount.Merged == true)
            {
                Contact mergedAccount =
                    (Contact)_serviceProxy.Retrieve(Contact.EntityLogicalName,
                    _account1Id, new ColumnSet(allColumns: true));
            }

Best Answer

That behaviour would be as expected - the Merge will move over child records for you from the subordinate to the master (so potentially opportunities, addresses etc.) but not try to workout which fields you want copied over. The reasoning (I would guess) is the potential business logic implications are endless - do you want to copy over emails? what if all email fields are filled? what about custom fields? And lots of other cases I'm sure everyone can think of.

Edited:

To workaround this, there is a property on the MergeRequest class called UpdateContent. If you update fields on this property, the values will be merged into the parent record.

You can actually see this in the link you has posted:

// Create another account to hold new data to merge into the entity.
// If you use the subordinate account object, its data will be merged.
Account updateContent = new Account();
updateContent.Address1_Line1 = "test";
Related Topic