Writing Create Request Using Web API

This article is about writing create request in Dynamics CRM 2016 using Web API.

In our earlier articles, we provided sample code to write retrieve and retrievemultiple request, today we are going to provide sample code to create entity record using Web API. Let's say we want to create account record and want to set different fields of different types. There are mainly two things which is different in Web API while creating entity record, the first is referring lookup field and the other is capturing response.

In Web API relationship is represented using Navigation properties, there are two types of navigation properties Single-valued and Collection-valued. Single-valued navigation is used to represent many to one (N:1) navigation whereas Collection-valued is used to represent one to many and many to many navigation property. You can find more details here.

To set the lookup (single-valued) navigation property we can use two options, like the following

  1. account["primarycontactid@odata.bind"]="/contacts(757B1E74-FBA3-E511-80DE-3863BB341BF0)";  

In above code, we are setting primary contact id using existing contact record, but if required we can also create contact record on the fly and set lookup using the following option:

  1. account["primarycontactid"]={"firstname":"Mahender","lastname":"Pal"};  

After create request is executed successfully, http response status code is returned as 204, which does not include any contents so if you will try to debug code, you will see response as blank like the following:


But we can get GUID of the new record using the following code :
  1. var accountUri = this.getResponseHeader("OData-EntityId");  
So let’s create a JavaScript web resource and use the following code under text editor:
  1. function createAccount() {  
  3.     var serverURL = Xrm.Page.context.getClientUrl();  
  4.     var account = {};  
  5.     account["name"] = "Web API Example";  
  6.     account["address1_city"] = "Delhi";  
  8.     //account["primarycontactid@odata.bind"]="/contacts(757B1E74-FBA3-E511-80DE-3863BB341BF0)";  //setting existing lookup  
  10.     account["primarycontactid"] = {  
  11.         "firstname""Mahender",  
  12.         "lastname""Pal"  
  13.     };  
  15.     //optionset  
  16.     account["industrycode"] = 1;  
  17.     //two options  
  18.     account["donotphone"] = true;  
  19.     //number  
  20.     account["numberofemployees"] = 20;  
  22.     var req = new XMLHttpRequest();  
  23.     req.open("POST", serverURL + "/api/data/v8.0/accounts"true);  
  24.     req.setRequestHeader("Accept""application/json");  
  25.     req.setRequestHeader("Content-Type""application/json; charset=utf-8");  
  26.     req.setRequestHeader("OData-MaxVersion""4.0");  
  27.     req.setRequestHeader("OData-Version""4.0");  
  28.     req.onreadystatechange = function() {  
  29.         if (this.readyState == 4 /* complete */ ) {  
  30.             req.onreadystatechange = null;  
  31.             if (this.status == 204) {  
  32.                 var accountUri = this.getResponseHeader("OData-EntityId");  
  33.                 var ID = accountUri.substr(accountUri.length - 38).substring(1, 37); //get only GUID  
  34.                 Xrm.Utility.openEntityForm("account", ID); //Open newly created account record  
  35.             } else {  
  36.                 var error = JSON.parse(this.response).error;  
  37.                 alert(error.message);  
  38.             }  
  39.         }  
  40.     };  
  41.     req.send(JSON.stringify(account));  
  42. }  

After that we can call this method on some event or through command button to create account record.

Stay tuned for more Web API Samples.