Change How Data is Handled in a Merge Operation in Dynamics CRM

Zoe Sands, 02 November 2015

When writing a plugin on the “Merge” message in Microsoft Dynamics CRM, there are three entities you need to work with – although the merge function in CRM is only performed on two records at a time. These are the “Master”, “Subordinate”, and “UpdateContent” entities.

image

When a merge is performed, a temporary record called “UpdateContent” is created. This record holds all the information that will appear in the final merged version. If I wanted to change how the information in the records was handled I would need to retrieve it from the Subordinate/Master records and set it in the “UpdateContent” record.

In this example the name of the subordinate account is added to the list of other names that an account is known by. The target entity is the Master record:

Guid subordinateid = (Guid)_context.InputParameters["SubordinateId"]; EntityReference targetReference = (EntityReference)_context.InputParameters["Target"]; Entity target = _sdk.Retrieve(targetReference.LogicalName, targetReference.Id, new ColumnSet("mag_othernames","mag_name")); Entity subordinate = _sdk.Retrieve("account", subordinateid, new ColumnSet("name")); Entity updateContentData = _context.InputParameters["UpdateContent"] as Entity; if (target == null || subordinate == null || updateContentData == null) { return; } if (_context.MessageName.Equals("merge", StringComparison.InvariantCultureIgnoreCase)) { string masterOther = target.Get<string>("mag_othernames"); string subordinateLegal = subordinate.Get<string>("name"); string masterLegal = target.Get<string>("name"); if (!masterLegal.Equals(subordinateLegal, StringComparison.InvariantCultureIgnoreCase)) { string otherNames = string.format(("{0}, {1}", masterOther, subordinateLegal); updateContentData["mag_othernames"] = otherNames; } }

When writing a plugin to alter how data is handled in the merge function, remember that changes are not applied to either of the existing records in the system but to the third temporary “UpdateContent” record. Plugins designed to change the handling of data when merge is run should be registered to run pre-operation so that all relative records are active and available.