Merging records on Dynamics 365 CRM is a very powerful feature that prevents duplicate records. I was asked to study this feature for a client who wanted to clean up his customer records, and we reached a point where we had to merge accounts that were related to active quotes. If you didn’t know it, Dynamics 365 For Sales disallows this operation by showing this error message:
After some investigations, I realized that an internal plugin of the Sales module prevents this operation. Actually, it is about the “Microsoft.Dynamics.Sales.Plugins.PreOperationAccountMerge” plugin.
This plugin is internal to Dynamics 365. I could not find out more from the default solution or from the Plugin registration tool. But it’s ok, everything can be shown on the “sdkmessageprocessingstep” table. I used the following fetchXml to see more details:
<fetch> <entity name="sdkmessageprocessingstep" > <link-entity name="plugintype" from="plugintypeid" to="eventhandler" > <filter> <condition attribute="name" operator="eq" value="Microsoft.Dynamics.Sales.Plugins.PreOperationAccountMerge" /> </filter> </link-entity> </entity> </fetch>
According to the result, I noticed that this plugin is running in pre-operation in order 1 for the Merge message:
So I tried to trick the merge validation plugin by using a new plugin that will run first, before the validation plugin. Its logic involves moving the related active offer from the subaccount to the Master account.
With this approach, the standard plugin will not find any active quotes on the subaccount. So the error will not be generated. Active quotes will be linked to the Master account, the merge action will then move the rest of the non-active quotes, and everyone will be happy!
This is possible by registering the new plugin with the following configuration:
- Message: Merge
- Event Pipeline Stage of Execution: PreOperation
- Execution Order: 0
As you can see, the plugin is executed on behalf of a specific user, not the calling user. Indeed, the plugin needs to update quotes that are active. This is only possible using the System Administrator security role only. You can see more details in the following documentation: Dynamics 365 Sales & GDPR
The code for the used plugin is as follows, please refer to the comments for more details.
Note that I use Power Platform Tools to generate and deploy the plugin… Hope it helps…