Dynamics 365 For Sales: How to merge Accounts if the subordinate record is associated with one or more active quotes?
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:
According to the result, I noticed that this plugin is running in pre-operation in order 1 for the Merge message:
Solution proposal
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Reblogged this on Nishant Rana's Weblog.
LikeLike
You are doing all the interesting stuffs Mehdi 👍. Thanks for sharing.
LikeLiked by 1 person