Create New Account Record using Web API in MS CRM 2015/16

Create new account record on click of custom button.


function CreateaccountRecord()
{
   var serverURL = Xrm.Page.context.getClientUrl();  
    var account= {};  
    account["name"] = "Rajeev Kumar";  
    account["address1_city"] = "Pune";  
  
    account["primarycontactid@odata.bind"]="/contacts(757B1E74-FBA3-E511-80DE-3863BB341BF0)";  //setting existing lookup  
  
    
  

  
    var req = new XMLHttpRequest();  
    req.open("POST", serverURL + "/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");  
                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(this.response).error;  
                alert(error.message);  
            }  
        }  
    };  
    req.send(JSON.stringify(account));  

}

Hope it Help!!

Comments

Popular posts from this blog

Workflow Activity Input and Output Parameters and Data Types in MS CRM 2013/15/16

Get Workflow Guid in Javascript using OData query

Convert Plugin to Sandbox mode,