Archive
Create record using OData and JQuery in CRM 2011
Hi,
Below is the code snippet to create a record (i.e., Account) using OData & JQuery.
//Prepare ‘Account’ object and call create function
function createAccount() {
//Create an object to represent an Account record and set properties
var account = new Object();
// Set text field
account.Name = “Rajeev Pentyala”;
// Set Lookup field (Contact should exists in the system)
var primaryContact = new Object();
primaryContact.ContactId = “”;
primaryContact.FullName = “”;
// if (primaryContact != null) {
// account.PrimaryContactId = { Id: primaryContact.ContactId, LogicalName: “contact”, Name: primaryContact.FullName };
// }
//Set a picklist value
account.PreferredContactMethodCode = { Value: 2 };
// Set a money value (i.e., Annual Revenue)
account.Revenue = { Value: “2000000.00” };
// Set a Decimal field (Here ‘new_DecimalField’ is my custom Account field)
account.new_DecimalField = 200.00.toString();
// Set a Boolean value
account.DoNotPhone = true;
// Set Date field (Here ‘new_DateField’ is my custom Account field)
var myDate = new Date();
myDate.setFullYear(1980, 12, 29);
account.new_DateField= myDate;
// Call create method by passing
// (i) Entity Object (i.e.,account in this case)
//(ii) Entity Set
//(iii)SuccessCallback function
// (iv) Error callback function
createRecord(account, “AccountSet”, createAccountCompleted, null);
}
// This callback method executes on succesful account creation
function createAccountCompleted(data, textStatus, XmlHttpRequest) {
var account = data;
alert(“Account created; Id: ” + account.AccountId.toString());
}
// 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 serverUrl = 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: 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
alert(“Error on the creation of record; Error – “+errorThrown);
}
});
}
How do I call this method :-
- Create a new .jscript file (i.e., “account.js”)
- Copy & Paste above code
- Add “account.js” as a webresource in CRM
- Add “Json2.js” & “jquery1.4.1.min.js” helper script files as webresources
- You can get the helper files from CRM 2011 SDk under path “\sdk\samplecode\js\restendpoint\jqueryrestdataoperations\jqueryrestdataoperations\scripts”
- Add “createAccount” function to form load method (Refer below)
- Save & Publish
- Open any account record and on loading of record our script fires and it creates new account name “Rajeev Pentyala”
Hope it helps 🙂