Wednesday, April 6, 2016

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!!

No comments:

Post a Comment

Get files of last hour in Azure Data Factory

  Case I have a Data Factory pipeline that should run each hour and collect all new files added to the data lake since the last run. What is...