Qualify Lead in Dynamics 365 using Xrm.WebApi

Follow my blog for more interesting topics on Dynamics 365, Portals and Power Platform. For training and consulting, write to us at info@xrmforyou.com
Qualify or convert leads (Dynamics 365 Sales) | Microsoft Docs
Qualifying a lead Dynamically using WebApi is a requirement which is so common across Dynamics 365 Sales implementation. Although there is a Qualify button on the Lead form, many a times we need to qualify a lead dynamically using Xrm Client API.
It’s been quite long since WebAPI has been introduced in Dynamics 365. We started with XMLHttpRequest construct to invoke the WebApi endpoints and now we have Xrm.WebApi methods to perform most of our WebAPI operations from client side.
So let’s see on how we can qualify a lead using Xrm.WebApi.execute method. We are going to invoke the Out of the box QualifyLead action endpoint for this.


Below is the code the complete code to do the same. In the below code, while qualifying the lead we are setting the value of CreateAccount to false and CreateContact to false as well. This is because we don’t want to create an account and contact during lead qualification. You can set the parameters accordingly.

var postData = {};
var entity = {};
entity.id = "6b4fa496-3ff2-e311-9864-a45d36fc5f1c"; // make sure when you put your lead id, it should be without the curly braces.
entity.entityType = "lead";
postData.entity = entity;

postData.CreateAccount = false;
postData.CreateContact = false;
postData.CreateOpportunity = true;
postData.Status = 3;
var request = {
    entity: parameters.entity,
    CreateAccount: parameters.CreateAccount,
    CreateContact: parameters.CreateContact,
    CreateOpportunity: parameters.CreateOpportunity,
    Status: parameters.Status,
   getMetadata: function () {
       return {
          boundParameter: "entity",
          parameterTypes: {
             "entity": {
                "typeName": "mscrm.lead",
                "structuralProperty": 5
             },
             "CreateAccount": {
                "typeName": "Edm.Boolean",
                "structuralProperty": 1
             },
             "CreateContact": {
                "typeName": "Edm.Boolean",
                "structuralProperty": 1
             },
             "CreateOpportunity": {
                "typeName": "Edm.Boolean",
                "structuralProperty": 1
             },
             "Status": {
                "typeName": "Edm.Int32",
                "structuralProperty": 1
             }
          },
          operationType: 0,
          operationName: "QualifyLead"
       };
    }
};
Xrm.WebApi.online.execute(qualifyLeadRequest).then(
    function success(result) {
       if (result.ok) {
          result.json().then(function (data) {
             var output = data.value[0];
 
            // created oppid
             var oppId = output.opportunityid;
 
            // created accountid
             var accId = output.accountid;
 
            // created contactid
             var contactid = output.contactid;
          });
       }
    },
    function (error) {
       console.log("Error: ", error.message);
    }
);

In the above code, also observe how I am parsing the JSON result back from the QualifyLead action. You can get the created opportunityid, contactid and accountid post successful lead qualification operation.
Hope this helps!
Debajit Dutta
(Business Solutions MVP)


Discover more from Debajit's Power Apps & Dynamics 365 Blog

Subscribe to get the latest posts sent to your email.

Discover more from Debajit's Power Apps & Dynamics 365 Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading