Maintaining Lookup References during Merge in Dynamics 365

Nick Chin, 08 June 2020

When merging records in Dynamics 365, all lookups referencing the deactivated record will be replaced with the master record.

For example: there is a Case for managing merged Accounts and you need to keep the original Account look up reference after the Accounts are merged.

Currently there is no way to change this configuration in Dynamics 365, even if the Relationship Behaviour has been set to Configurable Cascading, you can’t change the Merge setting because the dropdown list is disabled.

image

For this solution I chose to use a Plugin that runs on pre-update, because you can modify the lookup without changing the modified by user, thus keeping the audit history intact. Also I need to use a plugin because I can access to the Lookup values pre and post update.

For my example the case has a field called “Firm being merged”, which is an Account lookup field. The “Firm being merged” will be merged with the Customer.

image

When the two Accounts are merged it will trigger the update of all lookups with the reference to the deactivated Account in this instance “Barbara”.

Running the plugin on the Case pre-update and filtering on the firm being merged field (mag_firmbeingmergedid), allows us to trigger the plugin code for the merged lookup update.

The code will compare the “Target” from the input parameters, which is the changes to the Case and the Pre Image “pre” to compare the current field data.

Note: I an used “.Get”, which is an extension that Magnetism uses, without this you will need to check if the attribute exists and then use “.GetAttributeValue()”.

The Pre Image will contain the parameters: customerid, mag_firmbeingmergedid and mag_itemid

I only want to check Cases with an item (mag_item) of “Merge Firm”.

image

Then the important part is to compare the changes with the current record data.

If the “mag_firmbeingmergedid” has data and current “customerid” equals the same as the changed “mag_firmbeingmergedid”, then I know this is the merged update.

I will then update the target “mag_firmbeingmergedid” to the value of the existing “mag_firmbeingmergedid” to retain the original value.

image