Saturday, 5 March 2016

Create record in CRM 2016 using CRM web API with java script.

Create record in CRM 2016 using web API with java script.

Hello CRM Developers,
          Here is my new post, In this post I am going to to tell how to create record in dynamics CRM 2016 using CRM web API.

In new CRM 2016 release there is a new way introduced for operation like create, update, delete,retrieve, multiple retrieve etc, web API. CRM Organization service is going to obsolete in this CRM by Microsoft. Web API has been introduced so that communication between CRM and and other platform application can be done easily. New CRM web API is fully support JSON format.

Below is the sample java script code for creating the record in CRM using java script.

function cerateRecord(clientURL)
    //clientURL = your CRM organization URL.  
    var req = new XMLHttpRequest()"POST", encodeURI(clientURL + "/api/data/v8.0/accounts"), 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) {
                var accountUri = this.getResponseHeader("OData-EntityId");                
            else {
                var error = JSON.parse(this.response).error;
    var account = new Object();
    //String Field = "Test Neo";
    //Option Set field
    account.ownershipcode = 1;
    //Two option
    account.donotemail = true;
    account.creditlimit = 12345;
    //Whole number
    account.numberofemployees = 28;
    account["primarycontactid@odata.bind"] = "/contacts(C8944807-EFD9-E511-80EC-3863BB2E4DE0)"
    var response = req.send(JSON.stringify(account));

For further help you can leave a comment.

Enjoy the great technology Dynamics CRM. Good Luck .....!!!!

No comments: