RE: Deep insert using the web api to create Primary Contact from account doesn't associate account to the contact on the company name field
If you can generate guid and use it in deep insert, then you should be about to set both lookup fields. But this is not best pratice.
var accountId = "00000000-0000-0000-0000-000000000009"
var contactId = "00000000-0000-0000-0000-000000000010"
var record = {};
record = {
"accountid": accountId,
"name": "Sample Account",
"primarycontactid":
{
"firstname": "John",
"lastname": "Smith",
"contactid": contactId,
"parentcustomerid_account@odata.bind": "/accounts(" + accountId + ")"
}
}
var req = new XMLHttpRequest();
req.open("POST", Xrm.Utility.getGlobalContext().getClientUrl() + "/api/data/v9.2/accounts", true);
req.setRequestHeader("OData-MaxVersion", "4.0");
req.setRequestHeader("OData-Version", "4.0");
req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
req.setRequestHeader("Accept", "application/json");
req.setRequestHeader("Prefer", "odata.include-annotations=*");
req.onreadystatechange = function () {
if (this.readyState === 4) {
req.onreadystatechange = null;
if (this.status === 204) {
var uri = req.getResponseHeader("OData-EntityId");
var regExp = /\(([^)]+)\)/;
var matches = regExp.exec(uri);
var newId = matches[1];
console.log(newId);
} else {
console.log(this.responseText);
}
}
};
req.send(JSON.stringify(record));