Qualify Lead using Web API in Dynamics 365

By | November 22, 2016

Introduction

With the release of Dynamics 365, Microsoft has introduced many new actions using Web API. One of the important action that has been introduced in this release is “QualifyLead” in Web API.

Execution of this action using Web API qualifies a lead and creates a contact, account, and opportunity that will be linked to the originating lead record.

Below are the required parameters for executing this action:

  • CreateAccount: This is of Type Boolean i.e. value can be passed as true or false. It specifies whether an account should be created after qualifying a lead or not.
  • CreateContact: This is of Type Boolean i.e. value can be passed as true or false. It specifies whether a contact should be created after qualifying a lead or not.
  • CreateOpportunity: This is of Type Boolean i.e. value can be passed as true or false. It specifies whether an opportunity should be created after qualifying a lead or not.
  • Status: This is of Type Int and value can be passed as the value of the corresponding Status Reason for the Status Qualify of the lead entity.

Now let’s see how we can programmatically execute this request using Web API.

Code snippet:

function qualifyLead(leadId, clientUrl) {
    var functionName = "qualifyLead >>";
    var query = "";
    try {

        //Define the query to execute the action
        query = "leads(" + leadId.replace("}", "").replace("{", "") + ")/Microsoft.Dynamics.CRM.QualifyLead";

        //pass the parameters required
        var data = {
            "CreateAccount": true,
            "CreateContact": true,
            "CreateOpportunity": true,
            "Status":3
        };

        //Create request
        var req = new XMLHttpRequest();
        req.open("POST", clientUrl + "/api/data/v8.2/" + query, true);
        req.setRequestHeader("Accept", "application/json");
        req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
        req.setRequestHeader("OData-MaxVersion", "4.0");
        req.setRequestHeader("OData-Version", "4.0");

        req.onreadystatechange = function () {

            if (this.readyState == 4 /* complete */) {
                req.onreadystatechange = null;

                if (this.status == 200 || this.status == 204) {
                    //success callback this returns null since no return value available.
                    var result = JSON.parse(this.response);

                } else {
                    //error callback
                    var error = JSON.parse(this.response).error;
                }
            }
        };
        req.send(JSON.stringify(data));

    } catch (e) {
        throwError(functionName, e);
    }
}

Conclusion

Microsoft has introduced many new features and enhancements with the release of Dynamics 365. This blog focusses on one of the new actions introduced in Dynamics 365 i.e. “QualifyLead” in Web API.

Visit our blogs to keep yourself updated with latest Dynamics 365 features/news/technology!