Recently I have come across a question in Dynamics 365 CRM Community in which someone is asking to fetch duplicates from contact entity. I had suggested Power Automate to do so. But my instinct insisted me to try the same using plugin or custom script. So I had reproduced the same scenario as in the community question. I used custom action, JavaScript, and Plugin for that. And guess what? It worked!
I have followed the below steps for fetching and deactivating duplicates
- Create a Custom Action
- Create and Register the Plugin
- Call Custom Action using JavaScript
- Execution
- Summary
Create a Custom Action
Create a custom action inside the solution. To do so, click on + New inside the solution → Automation → Process → Action.
A prompt for creating new action slides from right, give proper Display name, select None for Add to, select blank for Start from and hit the create button in the bottom
It navigates to the classic page for configuring action, create two process arguments, one for input and another one for output. I have created InputRequest as a boolean type for Input direction (Required) and OutputResponse as a string for Output direction. Activate the Custom Action.
Create and Register the Plugin
I have created a plugin for fetching duplicate records using fetch XML and deactivate them. The plugin gets executed once the input direction(InputRequest) turns true. After the execution, the output direction returned to action. The below is my plugin code.
Create a new plugin assembly if you haven't done yet. While registering the plugin step, select your custom action as a Message as like the below image (I have added my custom action named cr1d9_DeactivateDuplicates). Select Event Pipeline Stage of Execution as Post Operation. Click on Register New Step.
Call Custom Action using JavaScript
Create a JavaScript web resource for making an HTTP request to the custom action. The action can be called by using the below URL structure.
https://{org_name}.crm8.dynamics.com/api/data/v9.2/{action_name}
We should pass the Input direction to the action with the HTTP POST request to the custom action. After completion of the action execution, we can get the output direction. The below is the JavaScript function for calling the custom action.
To trigger the JavaScript function, we need to call it in any of the event. As I want the event inside the view, so I have created a command button in the view. To do so, go to pages(in app maker) → click on the ellipse (three dots) near the view → select Edit Command bar → Select the view type. You'll get see the command editor page, create button by clicking on the +New button on left navigation menu.
After the button was added to the command bar, change Label, Icon, and Action as you need. I have JavaScript Web resource, so I chose JavaScript action. For JavaScript action, you have to add library and function name. Add parameter if any.
Execution
The below GIF shows the execution of fetching and deactivating duplicates.
Summary
I will always suggest using the Duplicate Detection Rule for avoiding duplications. But in some cases of data inserting via API, we cannot detect the duplicates priorly. For such kind of scenarios, we require this plugin in place. It is useful for deactivating the duplicates which are already inserted to the table. The plugin will be useful if the data count is less, because the execution time of the plugin should not exceed two minutes. Have a Good day!
Amazing work
ReplyDelete