Create an entity with simple data types using MS Dynamics CRM Web API
Microsoft has introduced a new version of its REST based API for CRM, this is more powerful than the old SOAP and OData APIs and has the ability to be used on a variety of devices and platforms.
Let’s take a look at how to create an ‘account’ record with simple data types including lookup. Please note that we are using ‘POST’ request for create and the plural name for the entity ‘accounts’ is being used.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
function addNewAccount() { var entity = {}; entity.accountnumber = "ACC-201608250720"; //Single line of text entity.address1_country = "USA"; //Single line of text entity.accountcategorycode = 2; //Option Set entity.donotsendmm = true; //Two Options entity.numberofemployees = 150;//Whole Number entity.creditlimit = Number(parseFloat(50000).toFixed(2)); //Currency entity.name = "ACC-JS-201608250720"; //Single line of text entity.lastonholdtime = new Date("08/31/2016 09:30:00").toISOString(); //Date entity.new_accountrate = Number(parseFloat(10.50).toFixed(2)); //Decimal Number //Lookup entity["transactioncurrencyid@odata.bind"] = "/transactioncurrencies(98468ADD-F665-E611-80EE-C4346BAC1344)"; //Currency entity["primarycontactid@odata.bind"] = "/contacts(6DA0E5B9-88DF-E311-B8E5-6C3BE5A8B200)" //Primary Contact var req = new XMLHttpRequest(); req.open("POST", Xrm.Page.context.getClientUrl() + "/api/data/v8.1/accounts", true); req.setRequestHeader("OData-MaxVersion", "4.0"); req.setRequestHeader("OData-Version", "4.0"); req.setRequestHeader("Accept", "application/json"); req.setRequestHeader("Content-Type", "application/json; charset=utf-8"); req.onreadystatechange = function () { if (this.readyState === 4) { req.onreadystatechange = null; if (this.status === 204) { var uri = this.getResponseHeader("OData-EntityId"); var regExp = /\(([^)]+)\)/; var matches = regExp.exec(uri); var newEntityId = matches[1]; Xrm.Utility.alertDialog("Account ceated with Id=" + newEntityId.toString()); } else { Xrm.Utility.alertDialog((this.responseText)); } } }; req.send(JSON.stringify(entity)); } |
And here is how to do it in Jquery.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
function addNewAccount() { var entity = {}; entity.accountnumber = "ACC-201608250720"; entity.address1_country = "USA"; entity.accountcategorycode = 2; entity.donotsendmm = true; entity.numberofemployees = 150; entity.creditlimit = Number(parseFloat(50000).toFixed(2)); entity.name = "ACC-JS-201608250720"; entity.lastonholdtime = new Date("09/01/2016 09:30:00").toISOString(); entity.new_accountrate = parseFloat(10).toFixed(2); //Lookup entity["transactioncurrencyid@odata.bind"] = "/transactioncurrencies(98468ADD-F665-E611-80EE-C4346BAC1344)"; //Currency entity["primarycontactid@odata.bind"] = "/contacts(6DA0E5B9-88DF-E311-B8E5-6C3BE5A8B200)" //Primary Contact $.ajax({ type: "POST", contentType: "application/json; charset=utf-8", datatype: "json", url: Xrm.Page.context.getClientUrl() + "/api/data/v8.1/accounts", data: JSON.stringify(entity), beforeSend: function (XMLHttpRequest) { XMLHttpRequest.setRequestHeader("OData-MaxVersion", "4.0"); XMLHttpRequest.setRequestHeader("OData-Version", "4.0"); XMLHttpRequest.setRequestHeader("Accept", "application/json"); }, async: true, success: function (data, textStatus, xhr) { var uri = xhr.getResponseHeader("OData-EntityId"); var regExp = /\(([^)]+)\)/; var matches = regExp.exec(uri); var newEntityId = matches[1]; Xrm.Utility.alertDialog("Account ceated with Id=" + newEntityId.toString()); }, error: function (xhr, textStatus, errorThrown) { Xrm.Utility.alertDialog(textStatus + " " + errorThrown); } }); } |
We will be exploring Web API further in our next blog(s).
For more help related to MS Dynamics CRM, Please Contact Us.
Leave a Comments