CRM 2011 JQuery OData REST Endpoints Create Record
In CRM 2011, we can create record easily using JQuery and OData. This will get triggered asynchronously using the ajax functionality.
Create Record function from the SDK:
To create new record, you just need to instantiate your object and call the function. The tricky part is when you want to assign Lookup field or OptionSetValue. You can get more info on this from the SDK itself. Here is an example:
Create Record function from the SDK:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | function createRecord(entityObject, odataSetName, successCallback, errorCallback) { var serverUrl = Xrm.Page.context.getServerUrl(); var ODATA_ENDPOINT = "/XRMServices/2011/OrganizationData.svc" ; //entityObject is required if (!entityObject) { alert( "entityObject is required." ); return ; } //odataSetName is required, i.e. "AccountSet" if (!odataSetName) { alert( "odataSetName is required." ); return ; } //Parse the entity object into JSON var jsonEntity = window.JSON.stringify(entityObject); //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 + "/" + odataSetName, 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) { if (successCallback) { successCallback(data.d, textStatus, XmlHttpRequest); } }, error: function (XmlHttpRequest, textStatus, errorThrown) { if (errorCallback) errorCallback(XmlHttpRequest, textStatus, errorThrown); else errorHandler(XmlHttpRequest, textStatus, errorThrown); } }); } function errorHandler(xmlHttpRequest, textStatus, errorThrow) { alert( "Error : " + textStatus + ": " + xmlHttpRequest.statusText); } |
To create new record, you just need to instantiate your object and call the function. The tricky part is when you want to assign Lookup field or OptionSetValue. You can get more info on this from the SDK itself. Here is an example:
1 2 3 4 5 6 7 8 9 10 | var opportunity = { CustomerId: { __metadata: { type: "Microsoft.Crm.Sdk.Data.Services.EntityReference" }, Id: <lookup record id>, LogicalName: <lookup record logical name> }, new_tonnageloss: "0" , new_originaltonnage: "100" }; createRecord(opportunity, "OpportunitySet" , createOpportunityCompleted, null ); |
Did you know how does the same thing with the close incident request? I would like to know how to write the sentences to create incident resolution record.
ReplyDeleteHello
ReplyDeleteNice example.
I want to know what is successCallback parameter? From where would I get these function references?
Regards,
Bhavika Patel
Hi Bhavika,
DeletesuccessCallback is basically a normal javascript function you want to call after the request has been processed. You just need to create that function and pass that function name into that parameter list :)
Regards,
Andreas