Modify Lead Qualification process in Dynamics CRM using C#: How to prevent Opportunity/ Account Creation and Refresh Lead Page On Lead Qualify

One condition on Lead Qualify could be like: If we don't want to create Opportunit/ Account or Contact on Lead Qualification that heppens by default. So for this secnario Below is the code which is required to stop creation of Opportunity/ Account on Qualifying Lead.

Note: Register Plugin on "QualifyLead" message and Pre-Operation

 public void Execute(IServiceProvider serviceProvider)  

     {
       AvoidOpportunityCreationOnLeadQualify(serviceProvider);
     }
 
     private void AvoidOpportunityCreationOnLeadQualify(IServiceProvider serviceProvider)
     {
       IPluginExecutionContext contextPlugin = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
       IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
       IOrganizationService serviceDefaultUser = factory.CreateOrganizationService(contextPlugin.UserId);
 
       OptionSetValue osvStateCode = new OptionSetValue();
       OptionSetValue osvStatusCode = new OptionSetValue();
       string sName = string.Empty;
       string sTargetAch = string.Empty;
       EntityReference erLeadId = null;
 
       try
       {
         erLeadId = (EntityReference)contextPlugin.InputParameters["LeadId"];
 
         Entity entLead = serviceDefaultUser.Retrieve("lead", erLeadId.Id, new ColumnSet(true));
 
         if (erLeadId.LogicalName != "lead")
         { return; }
 
         osvStateCode = entLead.GetAttributeValue<OptionSetValue>("statecode");
         osvStatusCode = entLead.GetAttributeValue<OptionSetValue>("statuscode");
 
         if (osvStateCode.Value == 0 && osvStatusCode.Value == 1)
         {
           contextPlugin.InputParameters["CreateOpportunity"] = false; // set to true by default
           contextPlugin.InputParameters["CreateAccount"] = false;   // set to true by default
         }
       }
 
       catch (InvalidPluginExecutionException ex)
       {
         throw new InvalidPluginExecutionException(ex.Message);
       }
       catch (Exception ex)
       {
         throw new InvalidPluginExecutionException(ex.Message);
       }
     }

Issue :: Above all code worked fine for me but the problem which I faced was On Lead Qualify the Form was not refreshing and Qualify button was still showing. SO for this I added below JavaScript code on Form OnSave event which refresh the form::

function refresh(pContext) {
    var SaveMode, SaveEventVal;

    // 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);
            }, 1);
        }
    }
}

Comments

Popular posts from this blog

Search data in Gridview on Textbox Key press event using JQuery in Asp.Net- C#

StateCode and StatusCode Values for mostly used entities in Microsoft Dynamics CRM 2013

Dumps for Microsoft Dynamics CRM MB2-703 Practice Exam Questions Free

How to import CSV files into DataTable in C#

How to show enlarge image when mouse hover on image or link in Asp.Net(c#) using JavaScript

go to top image