I've written some javascript for custom entities in dynamics online. On a "registration" form, users are presented with questions that must be answered. On save, "answer" records are created and connected to the current "registration." My code that actually created the record is as follows:
function sendAnswers (parsedAnswer) {
var count = parsedAnswer.length;
//Parse the entity object into JSON
var jsonAnswers = window.JSON.stringify(parsedAnswer);
var serverUrl = Xrm.Page.context.getClientUrl();
var ODATA_ENDPOINT = "/XRMServices/2011/OrganizationData.svc";
var odataSetName = "new_regquestionanswers"
var odataUri = serverUrl + ODATA_ENDPOINT + "/" + odataSetName + "Set";
for (var i = 0; i < count; i++) {
//Parse the entity object into JSON
var jsonAnswers = window.JSON.stringify(parsedAnswer[i]);
console.log(jsonAnswers);
//Asynchronous AJAX function to Create a CRM record using OData
$.ajax({ type: "POST",
contentType: "application/json; charset=utf-8",
datatype: "json",
url: odataUri,
data: jsonAnswers,
beforeSend: function (XMLHttpRequest) {
//Specifying this header ensures that the results will be returned as JSON.
XMLHttpRequest.setRequestHeader("Accept", "application/json");
},
success: function (data, textStatus, XmlHttpRequest) {
console.log("The answers successfully posted");
parent.Xrm.Page.ui.controls.get("new_eventregistrationrelationshipid").setDisabled(true);
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert("Something went wrong.");
console.log(XMLHttpRequest.responseText);
}
});
}
}
The parameter sent to sendAnswers is read from a hidden field and stored as json.
I'm having an odd issue -- Every time I log into dynamics and try to test this functionality, the very first attempt fails. Subsequent attempts to test succeed and everything works correctly. The error response text I get on the first attempt is:
Error processing request stream. Error encountered in converting the value from request payload for property 'Id' to type 'Guid', which is the property's expected type. See inner exception for more detail.
I am at a loss of where to go from here. Does anyone have any insight into why this first attempt fails, then everything goes as planned? Thanks for any help.
Aucun commentaire:
Enregistrer un commentaire