Asked by:
CRM 2011, error trying to create a case from JavaScript

Question
-
Hi,
I'm trying to use JavaScript (on the account entity) to create a new case. I really didn't have a good guess as to how I should try to do this, but I've been able to find plenty examples online where someone was trying to create a new Account using JavaScript. Should be simple enough to alter that code to do it with a case (incident) instead. Right? It seems not. I'm getting an error I can't figure out.
I will mention first that I've got json2, and jquery-1.5.min included.
Here's the function that I'm calling, which fails:
// JScript source code
function createDemographicCase() {
var context = Xrm.Page.context;
var serverUrl = context.getServerUrl();
var ODATA_ENDPOINT = "/XRMServices/2011/OrganizationData.svc";
var incident = new Object();
/////////////////////////////////////////////////////////////
// Specify the ODATA entity collection
var ODATA_EntityCollection = "/IncidentSet";
/////////////////////////////////////////////////////////////
// Define attribute values for the CRM object you want created
incident.Title= "Demographic Change";
incident.OwnerId = "939E11B8-8713-E411-A4E2-005056B32356";
incident.New_ContactType = "1";
incident.SubjectId= "8AC1C653-8DFD-E511-A878-005056B32ED4";
incident.StateCode = "0";
incident.OwnerIdType = "8";
incident.CustomerId = "7B70229A-D285-DE11-BC81-0050568F7B78";
//Parse the entity object into JSON
var jsonEntity = window.JSON.stringify(incident);
//Asynchronous AJAX function to Create a CRM record using OData
$.ajax({ type: "POST",
contentType: "application/json; charset=utf-8",
datatype: "json",
url: serverUrl + ODATA_ENDPOINT + ODATA_EntityCollection,
data: jsonEntity,
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) {
alert("success");
var NewCRMRecordCreated = data["d"];
alert("CRM GUID created: " + NewCRMRecordCreated.IncidentId);
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert("failure");
}
});
}
(The hard coded values are all valid.... so long as I'm handling those GUIDs correctly, but I believe I am. I will be passing parameters into the function to set various fields later. I just assigned those fields like this because they're the "required" fields - though I also don't believe I actually need all of them set for this to work. The original code for creating an Account worked, without setting all the required fields).
Anyway, the error this will return is: "Error processing request stream. The request should be a valid top-level resource object."
I'm assuming that with the case entity in particular I'm missing something that's required? Anyone have any ideas?
Wednesday, April 13, 2016 7:26 PM
All replies
-
Hi,
What happens if you just create a case with a title?
Regards
Rickard Norström Developer CRM-Konsulterna
http://www.crmkonsulterna.se
Swedish Dynamics CRM Forum: http://www.crmforum.se
My Blog: http://rickardnorstrom.blogspot.seThursday, April 14, 2016 6:21 AM -
We found below things from your code that is causing error while creating record,
- You cannot set look up field value as string value. In your code, you are trying to set the lookup field (subject, customer and owner) as string value. You can set the look up field value as shown below.
incident.CustomerId = { Id: "38BD055D-D7C9-E511-80F1-3863BB343AF8", LogicalName: "account", Name: "Fourth Coffee (sample)" };
- You have to pass Id, LogicalName and Name (not mandatory) for lookup field. You do not need to pass OwnerIdType.
- Also You are trying to set the StateCode to change the state of record using JavaScript. You have to use “SetState” request.
HTH
Sam
- Edited by Sam - Inogic Thursday, April 14, 2016 7:47 AM
Thursday, April 14, 2016 7:47 AM -
Sam,
Thanks very much. Setting the CustomerId how you suggested work. I am now successfully creating a case with just Title & CustomerId (proper logic needs to be written in to handle all the details need, like subject, etc) but this has solved my problem.
Rickard,
Thanks for your suggestion too. I'll just mention (in case anyone else is reading this, and stumped on the same thing) that only setting the title did not work. The error is much less ambiguous though, it will come back telling you that you must set the CutomerId.
Sean
Thursday, April 14, 2016 12:39 PM -
Hi Sean,
Glad that this helped. Would appreciate if you could close this thread by marking the appropriate response from the thread as the answer.
Thanks!
Sam
Dynamics CRM MVP | Inogic | http://www.inogic.com/blog | news at inogic dot com
If this post answers your question, please click "Mark As Answer" on the post and "Mark as Helpful"
- Edited by Sam - Inogic Friday, April 15, 2016 4:35 AM
Friday, April 15, 2016 4:33 AM