Featured Post

Web API Requests Series

Web API Series:  Create and Retrieve , Update and Delete , Retrieve Multiple , Associate/Disassociate , Impersonation , Run Workflows

02 March 2016

CRM 2016 - WEB API CRUD Operations (Part 3)

Following Part 1 and Part 2 here is how to "Update" and "Delete" using the new CRM WebAPI.

    this.updateRecord = function (id, object, entitySetName, successCallback, errorCallback) {

        var req = new XMLHttpRequest();
        req.open("PATCH", encodeURI(getWebAPIPath() + type + "(" + id + ")"), 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 == 204 || this.status == 1223) {
                    successCallback();
                }
                else {
                    errorCallback(bd_Utilities.WebAPI.errorHandler(this));
                }
            }
        };
        req.send(JSON.stringify(object));
    };

    this.deleteRecord = function (id, entitySetName, successCallback, errorCallback) {

        var req = new XMLHttpRequest();
        req.open("DELETE", encodeURI(getWebAPIPath() + type + "(" + id + ")", 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 == 204) {
                    successCallback();
                }
                else {
                    errorCallback(bd_Utilities.WebAPI.errorHandler(this));
                }
            }
        };
        req.send();
    };

No comments:

Post a Comment