I am trying to associate two records of custom entities - 'Custom entity 1' and 'Custome Entity 2' having N:N relationship on MS CRM 2016 online instance using Web API. I am getting the following error alert -
An unexpected ‘StartArray’ node was found when reading from the JSON reader. A ‘StartObject’ node was expected.
Following is the code snippet -
function associateRequest() {
var serverURL = Xrm.Page.context.getClientUrl();
var associate = {
"@odata.id": serverURL + "/api/data/v8.0/new_customentity1s(08C7365D-4BD1-E511-80EA-3863BB34BA88)"
};
var req = new XMLHttpRequest();
req.open("POST", serverURL + "/api/data/v8.0/new_customeentity2s(9A1EF77F-4BD1-E511-80EA-3863BB34BA88)/new_new_customentity1_new_customeentity2/$ref", true);
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 /* complete */) {
req.onreadystatechange = null;
if (this.status == 204) {
alert('Record Associated');
} else {
var error = JSON.parse(this.response).error;
alert(error.message);
}
}
};
req.send(associate);
}
The disassociation of the records is working fine.
I think you need to use JSON.stringify on the object you're posting: