Tuesday 20 November 2012

Qualify Lead automatically to Account, Contact and Opportunity using JavaScript

  1. Download XrmServiceToolkit. Extract the zip file.
  2. Then you have to add the following script to crm webresources:
  •             jquery.js
  •             json.js
  •             XrmServiceToolkit.js
Save the following code as javascript file. Then add it to the webresources.

function qualifyLeadRequest() {
    var requestMain = ""
xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\">";
    requestMain += "      <request i:type=\"b:QualifyLeadRequest\" xmlns:a=\"http://schemas.microsoft.com/xrm/2011/Contracts\" xmlns:b=\"http://schemas.microsoft.com/crm/2011/Contracts\">";
    requestMain += "        <a:Parameters xmlns:c=\"http://schemas.datacontract.org/2004/07/System.Collections.Generic\">";
    requestMain += "          <a:KeyValuePairOfstringanyType>";
    requestMain += "            <c:key>LeadId</c:key>";
    requestMain += "            <c:value i:type=\"a:EntityReference\">";
    requestMain += "              <a:Id>" + Xrm.Page.data.entity.getId() + "</a:Id>";
    requestMain += "              <a:LogicalName>lead</a:LogicalName>";
    requestMain += "              <a:Name i:nil=\"true\" />";
    requestMain += "            </c:value>";
    requestMain += "          </a:KeyValuePairOfstringanyType>";
    requestMain += "          <a:KeyValuePairOfstringanyType>";
    requestMain += "            <c:key>CreateAccount</c:key>";
    requestMain += "            <c:value i:type=\"d:boolean\" xmlns:d=\"http://www.w3.org/2001/XMLSchema\">true</c:value>";
    requestMain += "          </a:KeyValuePairOfstringanyType>";
    requestMain += "          <a:KeyValuePairOfstringanyType>";
    requestMain += "            <c:key>CreateContact</c:key>";
    requestMain += "            <c:value i:type=\"d:boolean\" xmlns:d=\"http://www.w3.org/2001/XMLSchema\">true</c:value>";
    requestMain += "          </a:KeyValuePairOfstringanyType>";
    requestMain += "          <a:KeyValuePairOfstringanyType>";
    requestMain += "            <c:key>CreateOpportunity</c:key>";
    requestMain += "            <c:value i:type=\"d:boolean\" xmlns:d=\"http://www.w3.org/2001/XMLSchema\">true</c:value>";
    requestMain += "          </a:KeyValuePairOfstringanyType>";
    requestMain += "          <a:KeyValuePairOfstringanyType>";
    requestMain += "            <c:key>Status</c:key>";
    requestMain += "            <c:value i:type=\"a:OptionSetValue\">";
    requestMain += "              <a:Value>3</a:Value>";
    requestMain += "            </c:value>";
    requestMain += "          </a:KeyValuePairOfstringanyType>";
    requestMain += "          <a:KeyValuePairOfstringanyType>";
    requestMain += "            <c:key>OpportunityCurrencyId</c:key>";
    requestMain += "            <c:value i:type=\"a:EntityReference\">";
    requestMain += "              <a:Id>89c14850-a5f3-e011-8606-1cc1dee89aa8</a:Id>";
    requestMain += "              <a:LogicalName>transactioncurrency</a:LogicalName>";
    requestMain += "              <a:Name i:nil=\"true\" />";
    requestMain += "            </c:value>";
    requestMain += "          </a:KeyValuePairOfstringanyType>";
    requestMain += "          <a:KeyValuePairOfstringanyType>";
    requestMain += "            <c:key>OpportunityCustomerId</c:key>";
    requestMain += "            <c:value i:type=\"a:EntityReference\">";
    requestMain += "              <a:Id>e2d97648-a5f3-e011-8606-1cc1dee89aa8</a:Id>";
    requestMain += "              <a:LogicalName>account</a:LogicalName>";
    requestMain += "              <a:Name i:nil=\"true\" />";
    requestMain += "            </c:value>";
    requestMain += "          </a:KeyValuePairOfstringanyType>";
    requestMain += "          <a:KeyValuePairOfstringanyType>";
    requestMain += "            <c:key>SourceCampaignId</c:key>";
    requestMain += "            <c:value i:type=\"a:EntityReference\">";
    requestMain += "              <a:Id>0ada7648-a5f3-e011-8606-1cc1dee89aa8</a:Id>";
    requestMain += "              <a:LogicalName>campaign</a:LogicalName>";
    requestMain += "              <a:Name i:nil=\"true\" />";
    requestMain += "            </c:value>";
    requestMain += "          </a:KeyValuePairOfstringanyType>";
    requestMain += "        </a:Parameters>";
    requestMain += "        <a:RequestId i:nil=\"true\" />";
    requestMain += "        <a:RequestName>QualifyLead</a:RequestName>";
    requestMain += "      </request>";
//    requestMain += "    </Execute>";

    XrmServiceToolkit.Soap.Execute(requestMain);
}

function qualify() {   
    qualifyLeadRequest(); 
    window.location.reload(true);
}

  1. Open VisualRibbonEditor Tool. Make a Connection with your CRM Server.
  2. Then Click Open. Entity Ribbon dialog will be Opened. There you have to select LEAD ribbon.
  3. Select ConverToLead button. After selected you need to click Action tab. Expand Javascript function.
  4. There you have to give a function name: qualify
  5. Library: /WebResources/new_filename



No comments:

Post a Comment