USING WEB API REQUEST CREATE, UPDATE, DELETE, RETRIEVE, RETRIEVE MULTIPLE, ASSOCIATE, DISASSOCIATE

//////CREATE THE RECORD////////////////

var entity = {};
entity.new_currency = Number(parseFloat(13666).toFixed(4));
entity["new_customer_account@odata.bind"] = "/accounts(F62765A4-941A-E711-8117-C4346BDC5E51)";
entity.new_datetime = new Date("04/12/2017").toISOString();
entity.new_decimal = parseFloat(22.223).toFixed(2);
entity.new_floatpoint = parseFloat(2.3215).toFixed(2);
entity["new_lookuponaccountfromcontact@odata.bind"] = "/contacts(D9058775-01F0-E611-812C-5065F38C8781)";
entity.new_optionset = 1;
entity.new_twooptionset = false;
entity.new_wholenumber = null;
entity.accountnumber = "";

var req = new XMLHttpRequest();
req.open("POST", Xrm.Page.context.getClientUrl() + "/api/data/v8.2/accounts", 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) {
            var uri = this.getResponseHeader("OData-EntityId");
            var regExp = /\(([^)]+)\)/;
            var matches = regExp.exec(uri);
            var newEntityId = matches[1];
        } else {
            Xrm.Utility.alertDialog(this.statusText);
        }
    }
};
req.send(JSON.stringify(entity));

//////////UPDATE THE RECORD/////////

var entity = {};
entity.new_currency = Number(parseFloat(13666).toFixed(4));
entity["new_customer_account@odata.bind"] = "/accounts(F62765A4-941A-E711-8117-C4346BDC5E51)";
entity.new_datetime = new Date("04/12/2017").toISOString();
entity.new_decimal = parseFloat(22.223).toFixed(2);
entity.new_floatpoint = parseFloat(2.3215).toFixed(2);
entity["new_lookuponaccountfromcontact@odata.bind"] = "/contacts(D9058775-01F0-E611-812C-5065F38C8781)";
entity.new_optionset = 1;
entity.new_twooptionset = false;
entity.new_wholenumber = null;
entity.accountnumber = "";

var req = new XMLHttpRequest();
req.open("PATCH", Xrm.Page.context.getClientUrl() + "/api/data/v8.2/accounts(F62765A4-941A-E711-8117-C4346BDC5E51)", 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.setRequestHeader("If-None-Match", "*");
req.onreadystatechange = function() {
    if (this.readyState === 4) {
        req.onreadystatechange = null;
        if (this.status === 204) {
            //Success - No Return Data - Do Something
        } else {
            Xrm.Utility.alertDialog(this.statusText);
        }
    }
};
req.send(JSON.stringify(entity));

////////DELETE THE RECORD/////////////

var req = new XMLHttpRequest();
req.open("DELETE", Xrm.Page.context.getClientUrl() + "/api/data/v8.2/accounts(52A69993-C80F-E711-8121-C4346BDCAEF1)", 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) {
        req.onreadystatechange = null;
        if (this.status === 204 || this.status === 1223) {
            //Success - No Return Data - Do Something
        } else {
            Xrm.Utility.alertDialog(this.statusText);
        }
    }
};
req.send();

/////RETRIEVE SINGLE RECORD//////

var req = new XMLHttpRequest();
req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v8.2/accounts(52A69993-C80F-E711-8121-C4346BDCAEF1)?$select=name,new_currency,new_currency_base,_new_customer_value,new_datetime,new_decimal,new_floatpoint,_new_lookuponaccountfromcontact_value,new_optionset,new_twooptionset,new_wholenumber", 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.setRequestHeader("Prefer", "odata.include-annotations=\"*\"");
req.onreadystatechange = function() {
    if (this.readyState === 4) {
        req.onreadystatechange = null;
        if (this.status === 200) {
            var result = JSON.parse(this.response);
            var name = result["name"];
            var new_currency = result["new_currency"];
            var new_currency_formatted = result["new_currency@OData.Community.Display.V1.FormattedValue"];
            var new_currency_base = result["new_currency_base"];
            var new_currency_base_formatted = result["new_currency_base@OData.Community.Display.V1.FormattedValue"];
            var _new_customer_value = result["_new_customer_value"];
            var _new_customer_value_formatted = result["_new_customer_value@OData.Community.Display.V1.FormattedValue"];
            var _new_customer_value_lookuplogicalname = result["_new_customer_value@Microsoft.Dynamics.CRM.lookuplogicalname"];
            var new_datetime = result["new_datetime"];
            var new_decimal = result["new_decimal"];
            var new_decimal_formatted = result["new_decimal@OData.Community.Display.V1.FormattedValue"];
            var new_floatpoint = result["new_floatpoint"];
            var new_floatpoint_formatted = result["new_floatpoint@OData.Community.Display.V1.FormattedValue"];
            var _new_lookuponaccountfromcontact_value = result["_new_lookuponaccountfromcontact_value"];
            var _new_lookuponaccountfromcontact_value_formatted = result["_new_lookuponaccountfromcontact_value@OData.Community.Display.V1.FormattedValue"];
            var _new_lookuponaccountfromcontact_value_lookuplogicalname = result["_new_lookuponaccountfromcontact_value@Microsoft.Dynamics.CRM.lookuplogicalname"];
            var new_optionset = result["new_optionset"];
            var new_optionset_formatted = result["new_optionset@OData.Community.Display.V1.FormattedValue"];
            var new_twooptionset = result["new_twooptionset"];
            var new_twooptionset_formatted = result["new_twooptionset@OData.Community.Display.V1.FormattedValue"];
            var new_wholenumber = result["new_wholenumber"];
            var new_wholenumber_formatted = result["new_wholenumber@OData.Community.Display.V1.FormattedValue"];
        } else {
            Xrm.Utility.alertDialog(this.statusText);
        }
    }
};
req.send();

//////////RETRIEVE MULTIPLE/////////////////////
var req = new XMLHttpRequest();
req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v8.2/accounts?$select=name,new_currency,new_currency_base,_new_customer_value,new_datetime,new_decimal,new_floatpoint,_new_lookuponaccountfromcontact_value,new_optionset,new_twooptionset,new_wholenumber&$filter=accountnumber ne null and  new_currency eq 123 and  _new_customer_value ne 52A69993-C80F-E711-8121-C4346BDCAEF1 and  new_datetime gt 2017-04-11T18:30:00.000Z and  new_decimal ge 123 and  new_floatpoint lt 321 and  _new_lookuponaccountfromcontact_value eq D9058775-01F0-E611-812C-5065F38C8781 and  new_optionset eq 1 and  new_wholenumber eq 5&$orderby=name asc&$count=true", 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.setRequestHeader("Prefer", "odata.include-annotations=\"*\",odata.maxpagesize=5000");
req.onreadystatechange = function() {
    if (this.readyState === 4) {
        req.onreadystatechange = null;
        if (this.status === 200) {
            var results = JSON.parse(this.response);
            var recordCount = results["@odata.count"];
            for (var i = 0; i < results.value.length; i++) {
                var name = results.value[i]["name"];
                var new_currency = results.value[i]["new_currency"];
                var new_currency_formatted = results.value[i]["new_currency@OData.Community.Display.V1.FormattedValue"];
                var new_currency_base = results.value[i]["new_currency_base"];
                var new_currency_base_formatted = results.value[i]["new_currency_base@OData.Community.Display.V1.FormattedValue"];
                var _new_customer_value = results.value[i]["_new_customer_value"];
                var _new_customer_value_formatted = results.value[i]["_new_customer_value@OData.Community.Display.V1.FormattedValue"];
                var _new_customer_value_lookuplogicalname = results.value[i]["_new_customer_value@Microsoft.Dynamics.CRM.lookuplogicalname"];
                var new_datetime = results.value[i]["new_datetime"];
                var new_decimal = results.value[i]["new_decimal"];
                var new_decimal_formatted = results.value[i]["new_decimal@OData.Community.Display.V1.FormattedValue"];
                var new_floatpoint = results.value[i]["new_floatpoint"];
                var new_floatpoint_formatted = results.value[i]["new_floatpoint@OData.Community.Display.V1.FormattedValue"];
                var _new_lookuponaccountfromcontact_value = results.value[i]["_new_lookuponaccountfromcontact_value"];
                var _new_lookuponaccountfromcontact_value_formatted = results.value[i]["_new_lookuponaccountfromcontact_value@OData.Community.Display.V1.FormattedValue"];
                var _new_lookuponaccountfromcontact_value_lookuplogicalname = results.value[i]["_new_lookuponaccountfromcontact_value@Microsoft.Dynamics.CRM.lookuplogicalname"];
                var new_optionset = results.value[i]["new_optionset"];
                var new_optionset_formatted = results.value[i]["new_optionset@OData.Community.Display.V1.FormattedValue"];
                var new_twooptionset = results.value[i]["new_twooptionset"];
                var new_twooptionset_formatted = results.value[i]["new_twooptionset@OData.Community.Display.V1.FormattedValue"];
                var new_wholenumber = results.value[i]["new_wholenumber"];
                var new_wholenumber_formatted = results.value[i]["new_wholenumber@OData.Community.Display.V1.FormattedValue"];
            }
        } else {
            Xrm.Utility.alertDialog(this.statusText);
        }
    }
};
req.send();

////////////ASSOCIATE THE RECORD//////////////
var association = {
    "@odata.id": Xrm.Page.context.getClientUrl() + "/api/data/v8.2/accounts(F62765A4-941A-E711-8117-C4346BDC5E51)"
};
var req = new XMLHttpRequest();
req.open("POST", Xrm.Page.context.getClientUrl() + "/api/data/v8.2/accounts(52A69993-C80F-E711-8121-C4346BDCAEF1)/new_account_account_customer/$ref", 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) {
        req.onreadystatechange = null;
        if (this.status === 204 || this.status === 1223) {
            //Success - No Return Data - Do Something
        } else {
            Xrm.Utility.alertDialog(this.statusText);
        }
    }
};
req.send(JSON.stringify(association));

/////////////////DISASSOCIATE THE RECORD///////////////

var req = new XMLHttpRequest();
req.open("DELETE", Xrm.Page.context.getClientUrl() + "/api/data/v8.2/accounts(52A69993-C80F-E711-8121-C4346BDCAEF1)/new_account_account_customer(F62765A4-941A-E711-8117-C4346BDC5E51)/$ref", false);
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) {
        req.onreadystatechange = null;
        if (this.status === 204 || this.status === 1223) {
            //Success - No Return Data - Do Something
        } else {
            Xrm.Utility.alertDialog(this.statusText);
        }
    }
};
req.send();

Comments

Popular posts from this blog

Meta Data Using WebApiRequest

Sample CRUD Operations Using C# in MSCRM

Basic Plugin Code in D365 using C#