Monday, March 7, 2016

CRM 2016 WEB API - Retrieve Specific Record

This is a helper function to retrieve and specific record in CRM 2016 using the WEB API

function RetrieveEntity2016(clientURL, entityId, entityType, query) {
    var req = new XMLHttpRequest();
    req.open('GET', clientURL + "/api/data/v8.0/"; + entityType + "(" + entityId + ")" + 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) {
                var data = window.JSON.parse(this.response);
                console.log("Retrieved " + entityType + " with entity id "+ data.accountid);
            }
            else {
                var error = window.JSON.parse(this.response).error;
                console.log(error.message);
            }
        }
    };
    req.send();
}

No comments:

Post a Comment