Tags

, , ,

Hello Everyone,

In my Previous Post I discussed about the Duplicate Detection Rule behavior in accordance with the Security role provided to the Users. The same scenario came in front of me during an Implementation of CRM at one of our clients.

They have two different teams under Sales which acts as child BU and each BU was given the role of read,write and create role at BU level only. Hence they were not allowed to see the other BU leads. Moreover they also had the requirement of data integrity at the Organization level; means the two different teams should not call or follow up with the same lead.

Now with the current Duplicate Detection Rule this was not possible, henceforth I finally wrote a plugin which I will be sharing with you here.

  • The first thing to keep in mind was running the plug-in as an Impersonated User.

Impersonation as described in MSDN is:

Impersonation is used to execute business logic (custom code) on behalf of a Microsoft Dynamics CRM system user to provide a desired feature or service for that user.

I found out the SystemUserId of the Administrator and used it in my plugin so that the Plugin should execute on behalf-of Administrator and check for duplicates at the Organization Level; instead of running at user level.

  • In the Second part, I used the FetchXML to find the leads with the Existing Mobile No.
String mob = (String)entity.Attributes["mobilephone"];

var fetchXml = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>";
fetchXml += "<entity name='lead'>";
fetchXml += "<attribute name='leadid' />";
fetchXml += "<filter type='and'>";
fetchXml += "<condition attribute='mobilephone' operator='eq' value='" + mob + "' />";
fetchXml += "</filter>";
fetchXml += "</entity>";
fetchXml += "</fetch>";
var result = service.RetrieveMultiple(new FetchExpression(fetchXml));
if (result.Entities.Count > 0)
{
throw new InvalidPluginExecutionException("The Mobile Number provided already Exists in CRM");
}

In case a duplicate Lead with the exact Mobile No is found in the Organization then a Alert is showed to the User regarding the duplicate with InvaliPluginExecutionExceptioin.  This will lead the plugin execution to fall under the Catch method which will stop the Duplicate Lead creation with the Error.

I’m sharing the Full Plugin that I used for this Duplication including Impersonation and Alert showing the Duplicate message.

namespace DuplicateDetection
{
    public class Class1:IPlugin
    {
        IOrganizationService service;
        public void Execute(IServiceProvider serviceProvider)
        {
            Guid impersonateId = new Guid("E76333D7-7A76-E211-B481-E41F1363AB2C"); // System User Id of Administrator 
            IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
            IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
            IOrganizationService service = serviceFactory.CreateOrganizationService(impersonateId);

            if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
            {
                Entity entity = (Entity)context.InputParameters["Target"];

                if (entity.LogicalName == "lead")
                {
                    if (entity.Contains("mobilephone"))
                    {
                        if ((entity.Attributes["mobilephone"]) != null)
                        {
                            try
                            {
                                String mob = (String)entity.Attributes["mobilephone"];

                                var fetchXml = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>";
                                fetchXml += "<entity name='lead'>";
                                fetchXml += "<attribute name='leadid' />";
                                fetchXml += "<filter type='and'>";
                                fetchXml += "<condition attribute='mobilephone' operator='eq' value='" + mob + "' />";
                                fetchXml += "</filter>";
                                fetchXml += "</entity>";
                                fetchXml += "</fetch>";
                                var result = service.RetrieveMultiple(new FetchExpression(fetchXml));
                                if (result.Entities.Count > 0)
                                {
                                    throw new InvalidPluginExecutionException("The Mobile Number provided already Exists in CRM");
                                }

                            }
                            catch (Exception ex)
                            {
                                throw new InvalidPluginExecutionException(ex.Message,ex);
                            }
                        }
                    }
                }
            }
        }
    }
}

Hope this small plugin will be helpful for others struggling with the Duplicate Issue.

Thanks !!!

😀

Advertisements