Share via


CRM 2013 Qualify Lead using Javascript

CRM 2013 Qualify Lead using Javascript  without creating opportunities,account,contact and manually create account:

In CRM 2013, Microsoft has given the qualify button which will directly convert into Opportunities it will be redirected.

Using the Plugins, it is not possible to redirect the page according to our needs. So i have found the method to Qualify the Lead using the Javascript with the “SuppressDuplicateDetection” Method which will allow to the duplicate you can set it as true(or) false.

To avoid need to use the Server Side Plugins.Check the Below code which will help and save time instead of using the plugins to Qualify the Lead.

Following Action will be Performed :

1. Lead will be Qualified.

2. After that Account will be created using the Javascript Method not using  the “CreateAccount” Method Which is set false.

function qualifyleadprocess() {

    var request = ""

    request += "<s:Envelope xmlns:s=\http://schemas.xmlsoap.org/soap/envelope/\>";

    request += "  <s:Body>";

    request += "    <Execute xmlns=\http://schemas.microsoft.com/xrm/2011/Contracts/Services\ xmlns:i=\http://www.w3.org/2001/XMLSchema-instance\>";

    request += "      <request i:type=\b:QualifyLeadRequest\ xmlns:a=\http://schemas.microsoft.com/xrm/2011/Contracts\ xmlns:b=\http://schemas.microsoft.com/crm/2011/Contracts\>";

    request += "        <a:Parameters xmlns:c=\http://schemas.datacontract.org/2004/07/System.Collections.Generic\>";

    request += "          <a:KeyValuePairOfstringanyType>";

    request += "            <c:key>LeadId</c:key>";

    request += "            <c:value i:type=\a:EntityReference\>";

    request += "              <a:Id>" + Xrm.Page.data.entity.getId() + "</a:Id>";

    request += "              <a:LogicalName>lead</a:LogicalName>";

    request += "              <a:Name i:nil=\true\ />";

    request += "            </c:value>";

    request += "          </a:KeyValuePairOfstringanyType>";

 

    request += "          <a:KeyValuePairOfstringanyType>";

    request += "            <c:key>CreateAccount</c:key>";

    request += "            <c:value i:type=\d:boolean\ xmlns:d=\http://www.w3.org/2001/XMLSchema\>false</c:value>";

    request += "          </a:KeyValuePairOfstringanyType>";

 

    request += "          <a:KeyValuePairOfstringanyType>";

    request += "            <c:key>CreateContact</c:key>";

    request += "            <c:value i:type=\d:boolean\ xmlns:d=\http://www.w3.org/2001/XMLSchema\>false</c:value>";

    request += "          </a:KeyValuePairOfstringanyType>";

 

    request += "          <a:KeyValuePairOfstringanyType>";

    request += "            <c:key>SuppressDuplicateDetection</c:key>";

    request += "            <c:value i:type=\d:boolean\ xmlns:d=\http://www.w3.org/2001/XMLSchema\>true</c:value>";

    request += "          </a:KeyValuePairOfstringanyType>";

 

    request += "          <a:KeyValuePairOfstringanyType>";

    request += "            <c:key>CreateOpportunity</c:key>";

    request += "            <c:value i:type=\d:boolean\ xmlns:d=\http://www.w3.org/2001/XMLSchema\>false</c:value>";

    request += "          </a:KeyValuePairOfstringanyType>";

 

    request += "          <a:KeyValuePairOfstringanyType>";

    request += "            <c:key>Status</c:key>";

    request += "            <c:value i:type=\a:OptionSetValue\>";

    request += "              <a:Value>3</a:Value>";

    request += "            </c:value>";

    request += "          </a:KeyValuePairOfstringanyType>";

    request += "        </a:Parameters>";

    request += "        <a:RequestId i:nil=\true\ />";

    request += "        <a:RequestName>QualifyLead</a:RequestName>";

    request += "      </request>";

    request += "    </Execute>";

    request += "  </s:Body>";

    request += "</s:Envelope>";

    //send set state request

    $.ajax({

        type: "POST",

        contentType: "text/xml; charset=utf-8",

        datatype: "xml",

        url: Xrm.Page.context.getServerUrl() + "/XRMServices/2011/Organization.svc/web",

        data: request,

        beforeSend: function (XMLHttpRequest) {

            XMLHttpRequest.setRequestHeader("Accept", "application/xml, text/xml, */*");

            XMLHttpRequest.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/xrm/2011/Contracts/Services/IOrganizationService/Execute");

        },

        success: function (data, textStatus, XmlHttpRequest) {

            var parentaccountid = null;

            parentaccountid = Xrm.Page.getAttribute("parentaccountid").getValue();

            if (parentaccountid != null) {

 

            }

 

            else {

 

                CreateAccountLead();

            }

 

        },

        error: function (XMLHttpRequest, textStatus, errorThrown) {

 

            alert(errorThrown) + 'Lead';

        }

    });

 

}

 

///Create Account Record Type-Customer

function CreateAccountLead() {

 

    var serverUrl = Xrm.Page.context.getServerUrl().toString();

    var ODATA_ENDPOINT = "/XRMServices/2011/OrganizationData.svc";

    var ODATA_EntityCollection = "/AccountSet";

 

    var objAccount = new Object();

    // set the name of Account

    objAccount.Name = Xrm.Page.getAttribute("fullname").getValue();

 

    objAccount.EMailAddress1 = Xrm.Page.getAttribute("emailaddress1").getValue();

 

    objAccount.CustomerTypeCode = { Value: 1 };

    objAccount.OriginatingLeadId = { Id: Xrm.Page.data.entity.getId(), LogicalName: "lead", Name: Xrm.Page.getAttribute("fullname").getValue() };

 

    objAccount.new_ethnicgroup = { Value: Xrm.Page.getAttribute("new_optethnicgroup").getValue() };

    objAccount.Telephone1 = Xrm.Page.getAttribute("mobilephone").getValue();// mobile phone

    objAccount.Telephone2 = Xrm.Page.getAttribute("telephone2").getValue();// // home phone

    objAccount.Telephone3 = Xrm.Page.getAttribute("telephone1").getValue();// office tel

 

    var jsonEntity = window.JSON.stringify(objAccount);

 

    $.ajax({

        type: "POST",

        contentType: "application/json; charset=utf-8",

        datatype: "json",

        url: serverUrl + ODATA_ENDPOINT + ODATA_EntityCollection,

        data: jsonEntity,

        beforeSend: function (XMLHttpRequest) {

            XMLHttpRequest.setRequestHeader("Accept", "application/json");

        },

        success: function (response) {

 

            if (response != null && response.d != null) {

 

                redirectaccount(response.d.AccountId);

            }

        },

        error: function (xmlHttpRequest, textStatus, errorThrown) {

 

            alert("Status: " + textStatus + "; ErrorThrown: " + errorThrown);

        }

    });

}

function redirectaccount(accountid) {

Xrm.Page.ui.close();

Xrm.Utility.openEntityForm("account", accountid);

 

 

}