Dynamics 365: Call global action with input and output parameters through JavaScript using XMLHttpRequest

1. I have created below action in dynamics 365.
2. new_CreatePerson action will create a new record for custom entity.
3.

4. Below is the JavaScript code to call the action.

var firstName;
var lastName;

function CreatePerson() {
    debugger;
    firstName = document.getElementById("firstName").value;
    lastName = document.getElementById("lasttName").value;
    var data =
    {
        "new_name": firstName + " " + lastName,
        "new_firstname": document.getElementById("firstName").value,
        "new_lastname": document.getElementById("lasttName").value
    }

    debugger;

    var req = new XMLHttpRequest();
    req.open("POST"Xrm.Page.context.getClientUrl() + "/api/data/v9.1/new_CreatePerson"true);
    req.setRequestHeader("OData-MaxVersion""4.0");
    req.setRequestHeader("OData-Version""4.0");
    req.setRequestHeader("Accept""application/json");
    req.setRequestHeader("Content-Type""application/json; charset=utf-8");

    req.onreadystatechange = function () {
        if (this.readyState === 4) {
            req.onreadystatechange = null;
            if (this.status === 204) {
                debugger;
                var results = JSON.parse(this.response);
            }
            if (this.status === 200) {
                debugger;
                var results = JSON.parse(this.response).id;
                Xrm.Utility.alertDialog("Record created with name: " + results);
            }
            else {
                debugger;
                Xrm.Utility.alertDialog(this.statusText);
            }
        }
    };
    req.send(JSON.stringify(data));
}

Comments

Popular posts from this blog

Call Dynamics 365 Web API Using JavaScript - Using XMLHttpRequest

Create related entity records along with the primary record in D365 using Web API Javascript - XMLHttpRequest