RE: Create a Record in Javascript in CRM 2015
Hi whydoihavetocreateanotherlogin,
This article explains how to create a record using OData and jQuery in Dynamics CRM 2015. You will need to edit the JavaScript to pass your desired parameters into the createRecord function. It will only take a small amount of editing to meet your requirement.
// This function creates record by making OData call
function createRecord(entityObject, odataSetName, successCallback, errorCallback) {
//Parse the entity object into JSON
var jsonEntity = window.JSON.stringify(entityObject);
// Get Server URL
var url = "";
if (Xrm.Page.context.getClientUrl) {
//Post UR 12
url = Xrm.Page.context.getClientUrl();
}
else {
//Pre UR 12
url = Xrm.Page.context.getServerUrl();
}
//The OData end-point
var ODATA_ENDPOINT = "/XRMServices/2011/OrganizationData.svc";
//Asynchronous AJAX function to Create a CRM record using OData
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
datatype: "json",
url: url 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 {
alert("An error has occured while creating the record; Error – " errorThrown);
}
}
});
}