Create an entity with related entities
In our earlier post we took a look at how to Create an entity with simple data types using MS Dynamics CRM Web API. Today we will create an entity with related entities. We will create the ‘primarycontact’ along with the ‘account’ record. We will also be creating an opportunity for the ‘account’ and related ‘task’ with the ‘opportunity’. Please note that for ‘lookup’, we use ‘odata.bind’ expression, but for related ‘contact’ we don’t use the ‘odata.bind’ expression. Also note the difference between single valued and collection valued properties, and multi values initialization and push functions both can be used for collection valued properties.
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 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
function addNewAccount() { var entity = {}; entity.accountnumber = "ACC-201609030730"; //Single line of text entity.address1_country = "USA"; //Single line of text entity.accountcategorycode = 2; //Option Set entity.name = "ACC-JS-201609030730"; //Single line of text //Lookup entity["transactioncurrencyid@odata.bind"] = "/transactioncurrencies(98468ADD-F665-E611-80EE-C4346BAC1344)"; //Currency //adding related contact var contact = {}; contact["firstname"] = "CON-JS"; contact["lastname"] = "201609030730"; //Primary Contact entity["primarycontactid"] = contact; //Single Valued Property var opportunity = {}; opportunity["name"] = "OPP-JS-201609030730"; var opportunityTask = {}; opportunityTask["subject"] = "First Task Associated to OPP-JS-201609030730"; var secondOpportunityTask = {}; secondOpportunityTask["subject"] = "Second Task Associated to OPP-JS-201609030730"; var thirdOpportunityTask = {}; thirdOpportunityTask["subject"] = "Third Task Associated to OPP-JS-201609030730"; opportunity["Opportunity_Tasks"] = [opportunityTask, secondOpportunityTask]; //Collection Valued Property opportunity["Opportunity_Tasks"].push(thirdOpportunityTask); entity["opportunity_customer_accounts"] = [opportunity]; //Collection Valued Property 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 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
function addNewAccount() { var entity = {}; entity.accountnumber = "ACC-201609030730"; //Single line of text entity.address1_country = "USA"; //Single line of text entity.accountcategorycode = 2; //Option Set entity.name = "ACC-JS-201609030730"; //Single line of text //Lookup entity["transactioncurrencyid@odata.bind"] = "/transactioncurrencies(98468ADD-F665-E611-80EE-C4346BAC1344)"; //Currency //adding related contact var contact = {}; contact["firstname"] = "CON-JS"; contact["lastname"] = "201609030730"; //Primary Contact entity["primarycontactid"] = contact; //Single Valued Property var opportunity = {}; opportunity["name"] = "OPP-JS-201609030730"; var opportunityTask = {}; opportunityTask["subject"] = "First Task Associated to OPP-JS-201609030730"; var secondOpportunityTask = {}; secondOpportunityTask["subject"] = "Second Task Associated to OPP-JS-201609030730"; var thirdOpportunityTask = {}; thirdOpportunityTask["subject"] = "Third Task Associated to OPP-JS-201609030730"; opportunity["Opportunity_Tasks"] = [opportunityTask, secondOpportunityTask]; //Collection Valued Property opportunity["Opportunity_Tasks"].push(thirdOpportunityTask); entity["opportunity_customer_accounts"] = [opportunity]; //Collection Valued Property $.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); } }); } |
For more help related to MS Dynamics CRM, Please Contact Us.
Leave a Comments