Friday, 20 May 2016

Create Record in Microsoft Dynamics CRM 2016 using Web API


  function CreateAccount() {

    var serverURL = Xrm.Page.context.getClientUrl();
    var account = {};
    account["name"] = "Create record Web API Example"; //set single line of text field
    account["address1_city"] = "Islamabad";//set single line of text field
    //Lookup
    account["primarycontactid@odata.bind"] = "/contacts(0D7AFC02-891C-E611-80E1-5065F38B0371)";  //setting existing lookup
    //Below code will create contact on fly and set in primarycontact 
    //account["primarycontactid"] = {
    //    "firstname": "Mahender",
    //    "lastname": "Pal"
    //};

    //optionset
    account["industrycode"] = 1; // set optionSet field
    //two options
    account["donotphone"] = true; //set boolean field
    //number
    account["numberofemployees"] = 20;//set whole no field
    //date time
    //account["him_approvaldate"] = new Date();

    var req = new XMLHttpRequest();
    req.open("POST", serverURL + "/api/data/v8.0/accounts", 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.send(JSON.stringify(account));

    if (req.readyState == 4 /* complete */) {
        if (req.status == 204) {
            var accountUri = this.getResponseHeader("OData-EntityId");
            var ID = accountUri.substr(accountUri.length - 38).substring(1, 37); //get only GUID
           // Xrm.Utility.openEntityForm("account", ID); //Open newly created account record
        } else {
            var error = JSON.parse(req.response).error;
            alert(error.message);
        }
    }
}
 

No comments:

Post a Comment

Read only field , get latest value from read only field in in power app portal

Below line is used to make field readonly in power app portal through js $( "#bdo_relationshiprole_name" ).parent().css( "po...