Thursday, 27 December 2012

Qualify Lead ribbon button behaviour overriding.

I have seen some of the posts on MS CRM forum regrading the "Qualify Lead" button behaviour.
Some fellow are interested in the removal of the "Qualify Lead" dialog altogether.
In this post I am going to override the behaviour of the Lead Qualify button.

Customization aim:To remove the "Convert Lead" dialog and qualify the lead into "Account","Contact" and "Opportunity" whenever user clicks qualify lead ribbon button.

Question:What things needs to be customized in order to achieve this.

1)We need to customize the ribbondefinition of lead entity i.e  RibbonDiffXml.
2)We need to create one custom field on Lead form.
3)We need to write a JavaScript function.
4)We have to write a plugin which will take care of creation of account,contact and opportunity.

Solution steps:

1.Customizing the Lead ribbon button :

1.1) Create a solution containing lead entity.Export it and save it.Extract the solution file.
        Open the "customizations.xml" search the <RibbonDiffXml> tag.
        Replace this with the following

        <CustomActions >
          <CustomAction Id="Mscrm.Form.lead.ConvertLead.CustomAction" Location="Mscrm.Form.lead.ConvertLead" Sequence="100">
              <Button Id="Mscrm.Form.lead.ConvertLead" ToolTipTitle="$Resources:Ribbon.Form.opportunity.MainTab.Actions.Convert" ToolTipDescription="$Resources:Ribbon.Tooltip.ConvertLead" Command="Mscrm.Form.lead.Convert" Sequence="5" Alt="$Resources:Ribbon.Form.opportunity.MainTab.Actions.Convert" LabelText="$Resources:Ribbon.Form.opportunity.MainTab.Actions.Convert" Image16by16="/_imgs/ribbon/ConvertLead_16.png" Image32by32="/_imgs/ribbon/ConvertLead_32.png" TemplateAlias="o1" />
          <RibbonTemplates Id="Mscrm.Templates"></RibbonTemplates>
        <CommandDefinitions >
          <CommandDefinition Id="Mscrm.Form.lead.Convert">
            <EnableRules />
              <DisplayRule Id="Mscrm.LeadIsOpen" />
              <DisplayRule Id="Mscrm.CanWriteLead" />
              <!--<JavaScriptFunction FunctionName="convertLead" Library="/_static/sfa/leads/lead.js" />-->
              <JavaScriptFunction FunctionName="convertLead" Library="$webresource:new_CustomRibbonJavascript" />
          <TabDisplayRules />
          <DisplayRules >
            <DisplayRule Id="Mscrm.LeadIsOpen">
              <FormStateRule State="Existing" />
            <DisplayRule Id="Mscrm.CanWriteLead">
              <EntityPrivilegeRule EntityName="lead" PrivilegeType="Write" PrivilegeDepth="Basic" />
          <EnableRules />
        <LocLabels />

Note: In the above XML  default functionality is overwritten by a new webresource called new_CustomRibbonJavascript.

2.Lead form Customization:

Customize the lead form create one radio button with logical name "new_isleadqualified" having default value false.Hide this field on the form.We will make use of this field for writing our logic.

3.Write the Javascipt function:
Create a webresource  called "new_CustomRibbonJavascript" and add the following JavaScript function to it.

function convertLead() {
    if (Xrm.Page.getAttribute("new_isleadqualified").getValue() != null && Xrm.Page.getAttribute("new_isleadqualified").getValue() == false) {
        var answer = confirm("Do you want to qualify the lead");
        if (answer) {

4.Writing the plugin:
Create a plugin as given below

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Crm.Sdk;
using Microsoft.Xrm.Sdk;
using System.ServiceModel;
using System.Diagnostics;
using System.Data.Services;
using System.Data.Services.Client;
using contoso;
using System.Xml;
using Microsoft.Crm.Sdk.Messages;
namespace Xrm.Lead
    public class LeadPostUpdate : IPlugin
        public void Execute(IServiceProvider serviceProvider)
            Entity lead;
            IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
            if (context.InputParameters.Contains("Target") && context.InputParameters["Target"is Entity)
                //Verify that the entity represents a Lead
                if (((Entity)context.InputParameters["Target"]).LogicalName != "lead" || context.MessageName != "Update")
                    lead = (Entity)context.InputParameters["Target"];
            string msg = string.Empty;
                IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
                IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
                if (context.Depth == 1)
                    Entity updatelead = new Entity("lead");
                    updatelead["new_isleadqualified"] = false;
                    updatelead["leadid"] = lead.Id;                 
                    QualifyLeadRequest req = new QualifyLeadRequest();
                    req.CreateAccount = true;
                    req.CreateContact = true;
                    req.CreateOpportunity = true;
                    EntityReference currency = new EntityReference();
                    currency.LogicalName = "transactioncurrency";
                    currency.Id = new Guid("2D2983F1-9A4A-E211-B48A-1CC1DEE8CEB7");//GUID of the Currency being Used
                    req.OpportunityCurrencyId = currency;
                    req.SourceCampaignId = null;
                    req.OpportunityCustomerId = null;
                    req.LeadId = new EntityReference("lead", lead.Id);
                    req.Status = new OptionSetValue(3);
                    var res = (service.Execute(req) as QualifyLeadResponse);  
            catch (FaultException<OrganizationServiceFault> ex)
                throw new InvalidPluginExecutionException("An error occurred in the plug-in.", ex);
            catch (Exception ex)
                throw ex;

5.Register the plugin as per the below screen shots:

After registration of plugin  we are done with our desired task of overwriting the Lead Qualify ribbon

Below are the screen shots of the result.

I hope it will be useful to implement this for lead.Similarly we can override the Opportunity and Case.

Thanks and Regards,