Update record using OData and JQuery in CRM 2011
Hi,
Below is the script to update a record using OData & JQuery.
I am updating an Account record in this example.
function updateRecord(id, entityObject, odataSetName) {
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 Update a CRM record using OData
$.ajax({
type: “POST”,
contentType: “application/json; charset=utf-8”,
datatype: “json”,
data: jsonEntity,
url: serverUrl + ODATA_ENDPOINT + “/” + odataSetName + “(guid'” + id + “‘)”,
beforeSend: function (XMLHttpRequest) {
//Specifying this header ensures that the results will be returned as JSON.
XMLHttpRequest.setRequestHeader(“Accept”, “application/json”);
//Specify the HTTP method MERGE to update just the changes you are submitting.
XMLHttpRequest.setRequestHeader(“X-HTTP-Method”, “MERGE”);
},
success: function (data, textStatus, XmlHttpRequest) {
alert(“Updated successfully”);
},
error: function (XmlHttpRequest, textStatus, errorThrown) {
if (XmlHttpRequest && XmlHttpRequest.responseText) {
alert(“Error while updating ” + odataSetName+ ” ; Error – ” + XmlHttpRequest.responseText);
}
}
});
}
How do I update my Account record :-
- Prepare an account object and call above method
var accountId=””; /*Set Account record GUID*/
//Create an object to represent an Account record and set properties
var account = new Object();
account.Name = “Updated”;
account.Telephone1 = “1234567890”;
account.AccountNumber = “Updated-123”;
account.EmailAddress1 = rajeevpentyala@live.com;
// Set Date field (Here ‘new_DateField’ is my custom Account field)
var myDate = new Date();
myDate.setFullYear(1980, 12, 29);
account.new_DateField= myDate;
***Imp Note :-
- Provide property names in Pascal convention (Ex – AccountNumber,EmailAddress1 etc…);
- Tip is, if you have OData Query Designer installed, copy the column names of your entity and paste it here.
//Call above function by passing below Parameters
// (i) Update record GUID (i.e.,account Id this case)
// (ii) Entity Object (i.e.,account in this case)
//(iii) Entity Set (i.e., Accountset)
updateRecord(accountId, account, “AccountSet”);
Note :- Please make sure to add “Json2.js” & “jquery1.4.1.min.js” helper script files as webresources
Hope it helps 🙂
Hi,
How to set a DAte field in the account object?
account.DateFieldName = ????;
Thank You.
Charles
Hi,
Following to my previous post.
How to set a DateTime field in the account object?
account.DateTimeFieldName = ????;
Thank You.
Charles
You can set date field as below
var myDate = new Date();
myDate.setFullYear(1980, 12, 29);
account.DateTimeFieldName= myDate;
Thank you for your reply.
I already tried it but it always giving me the following error on error function “Incorrect attribute value type System.Data.Services.Serializers.JsonReader+JsonObjectRecords” after trying the below:
1. var myDate = new Date();
myDate.setFullYear(1980, 12, 29);
account.new_ApprovalDate = myDate;
2. var myDate = new Date(Xrm.Page.getAttribute(“new_approvaldate”).getValue());
account.new_ApprovalDate = myDate;
3. account.new_ApprovalDate = Xrm.Page.getAttribute(“new_approvaldate”).getValue();
after I used IE Developer Tool i noticed that the datetime field is always null {“new_ApprovalDate”:{}}
any hints on this error?
Thank you again.