Tags

, , , , , ,

Hello Everyone,

Lead Qualification process changed a bit in CRM 2013. Earlier in CRM 2011 we used to have a dialog box with options to choose from Account, Contact and Opportunity as in what all we would like to create on Lead Qualification.

But we don’t have that option available anymore in CRM 2013. Instead, inside the form there exists two fields as “Existing Contact” and “Existing Account” which are available to add Related Contact or Account already available. Based on the combination matrix mentioned here it creates New Account, Contact and Opportunity.

Even if you have not filled up those fields, still those two fields will pop up during the Lead Qualification to check for Duplicates depending on the Duplicate Detection Rule applied for Account and Contact.

Now this whole thing has been designed quite meaningfully by Microsoft but it doesn’t apply in every Industry. Like in my current Client Industry they do not require to create new Opportunity (This can even be handled through proper Rights/Roles on Opportunity). There was a need to replicate this functionality with the same set of Account and Contact in Lead. Moreover, they also wanted to suppress the Duplicate Detection check as it doesn’t fit their requirement somehow. Hence, keeping all these points in mind I needed to write a Business Logic that fulfills their requirement. I found a very nice blog here which pointed in the right direction to the solution.

I wrote the plugin code mentioned below for my scenario:-

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Client;
using Microsoft.Xrm.Sdk.Query;
using Microsoft.Xrm.Sdk.Messages;
using Microsoft.Xrm.Sdk.Discovery;
using Microsoft.Crm.Sdk.Messages;
using System.ServiceModel;
using System.Net;
using System.ServiceModel.Description;
//using System.Web.Services.Protocols;
using System.Xml;

namespace LeadQualification
{
    public class Class1:IPlugin
    {
        IOrganizationService service;

        public void Execute(IServiceProvider serviceProvider)
        {
            IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
            IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
            service = serviceFactory.CreateOrganizationService(context.UserId);

            // Get the qualified lead
            EntityReference leadid = (EntityReference)context.InputParameters["LeadId"];
            ColumnSet attributes2 = new ColumnSet(new string[] { "new_contact", "new_existingcustomer" });
            Entity lead = service.Retrieve(leadid.LogicalName, leadid.Id, attributes2);
            try
            {
                
                    if (lead.Contains("new_contact") && lead.Contains("new_existingcustomer") && lead.Attributes["new_contact"] != null && lead.Attributes["new_existingcustomer"] != null)
                    {
                        context.InputParameters["CreateOpportunity"] = false; 
                        context.InputParameters["CreateAccount"] = false; 
                        context.InputParameters["CreateContact"] = false; 
                        context.InputParameters["SuppressDuplicateDetection"] = true; 
                    }

                    else if ((!lead.Contains("new_contact")) && lead.Contains("new_existingcustomer") && lead.Attributes["new_existingcustomer"] != null)
                    {
                        context.InputParameters["CreateOpportunity"] = false; 
                        context.InputParameters["CreateAccount"] = false; 
                        context.InputParameters["CreateContact"] = true; 
                        context.InputParameters["SuppressDuplicateDetection"] = true;
                    }

                    else
                    {
                        context.InputParameters["SuppressDuplicateDetection"] = true; 
                        context.InputParameters["CreateOpportunity"] = false; 
                    }
               
            }

            catch (Exception ex)
            {
                throw new InvalidPluginExecutionException("An error occurred in the Plugin." + ex, ex); 
            }

        }

        private static string GetErrorCode(XmlNode errorInfo)
        {
            XmlNode code = errorInfo.SelectSingleNode("//code");

            if (code != null)
                return code.InnerText;
            else
                return "";
        }

    }
}

I then registered my plugin with ‘QualifyLead’ Message on Execution Pipeline as ‘Pre-Operation’.  And the functionality worked fine as expected.

The only one challenge that I faced using this way was, after I hit the Qualify Lead Button, operation used to work fine in the Background but the Lead page gets Freezed up. So I needed a way to Refresh the Lead Page and also the Parent Form i.e. the Grid from where the lead form was opened.

For the solution I wrote a webresource of type JScript with the following code.

function OnSave(pContext)
 {  
    var SaveMode, SaveEventVal;
    // debugger;
    // Change the Save Event Value as per required Save Event
    SaveEventVal = 16;

    if (pContext != null && pContext.getEventArgs() != null)
    {

        SaveMode = pContext.getEventArgs().getSaveMode();

        // 16 will pass on Lead Qualify button click
        if (SaveMode == SaveEventVal)
       {
         setTimeout(function()
                  {   
                      window.location.reload(true);
                      window.top.parent.document.location.reload(true);
                      
                   }, 2000);
          
        }
    }
}

 

I called the function during OnSave Event where this Script actually calls in for a reload of both the forms and parent Grid after a wait of 2 Seconds when Qualify Lead button is hit. This will solve the above mentioned issue and all-together they worked quite well.

Hope this will be helpful.

Thanks !!!

😀