Home

Showing posts with label create using javascripts. Show all posts
Showing posts with label create using javascripts. Show all posts

Wednesday, September 13, 2017

Create Entity using Web API in Dynamics 365

Creating an account record with name "sample account" using web api's
https://msdn.microsoft.com/en-us/library/gg328090.aspx
// Request 
POST [Organization URI]/api/data/v8.2/accounts HTTP/1.1
Content-Type: application/json; charset=utf-8
OData-MaxVersion: 4.0
OData-Version: 4.0
Accept: application/json
{
    "name": "Sample Account",  
}
// Response
HTTP/1.1 204 No Content
OData-Version: 4.0
OData-EntityId: [Organization URI]/api/data/v8.2/accounts(GUID)
---------------------------------------------------------------------------------------------------
function createEntity() {
    var entityName = "accounts";// Entity Name
    var clientURL = Xrm.Page.context.getClientUrl();// To get CRM client URl 
    var req = new XMLHttpRequest(); //Creating New HTTP request

    req.open("POST", encodeURI(clientURL + "/api/data/v8.2/" + entityName, 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) {/* Status 204 denotes success */
                alert("account created from api");
            }
            else {
                var error = JSON.parse(this.response).error;
                alert(error.message);
            }
        }
    };

    req.send(JSON.stringify(// json object to be created
        {
            name: "sample account"
        }));

}

Convert subgrid to Comments