This article continues to introduce the interface of two Web APIs, one is "create related entities in one operation" is the creation of the primary entity and associated entities in a single step operation, one is "Associate entities on Create "Fills the lookup field when the record is created.
The first API, the usual code, a little explanation, note the following points do not exist in order, I just separate explanation
1. Create an account entity record here
2. Create a contact record called "John Smith" (note that this record is new, not already exists), assign to the Primarycontactid (lookup field associated with the Contacts) attribute field in account
3, create a contact record called "Test Contacts", the Account record and this test contact record is 1:n relationship, that is, here the contacts record can create more than one
4, in the 3rd in the contact record created, and then associated with the task record, the principle of the 3rd
var entity =new Object (); entity["name"] = ' Create related entities ';//text Var contact=new Object (); contact["FirstName"]= "John"; contact["LastName"]= "Smith"; entity["Primarycontactid"]=contact; var contact_customer_accounts=new Array (); Contact_customer_accounts[0]=new Object (); contact_customer_accounts[0]["FirstName"]= "Contact"; contact_customer_accounts[0]["LastName"]= "Test"; var contact_tasks=new Array (); Contact_tasks[0]=new Object (); contact_tasks[0]["Subject"]= "Task associated to contact"; contact_customer_accounts[0]["Contact_tasks"]=contact_tasks; entity["Contact_customer_accounts"]=contact_customer_accounts; var jsonentity = window. Json.stringify (entity); var req = new XMLHttpRequest () Req.open ("Post", Xrm.Page.context.getClientUrl () + "/api/data/v8.0/accounts", false); 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) {if (This.status = = 204) { } else {var data=json.parse (this.responsetext). Error.message; } } }; Req.send (jsonentity);
The origin of the contact_customer_accounts in the code is as follows, Contact_tasks is also found in the contact, the schema name attention to case
There is a picture of the truth, on
The Account Master entity record created, and the contact for the lookup field of "John Smith" is also the new in this operation
Account records the associated multiple (I've created only one) Contact records
Test a contact record with multiple created (I've created only one) task record
The above is a create operation data generated in the record, is not very cool. This scenario is generally used in the background code is more, the previous practice is to create a master record through the organization service, get the master record GUID and then create sub-records, now an API once request can be done.
About the second API "Associate entities on Create", here is not much elaboration, with the above demo for example, is to create an Account record to primarycontactid this lookup field assignment , the actual process of using a few, even if you want to use it is directly included in the Create API.
Dynamics CRM2016 Web API Create related entities in one operation