Archive
Deleting a record using OData & JQuery in CRM 2011
Hi,
Below is the script to delete record using OData & JScript
function deleteRecord(id, odataSetName) {
// Get Server URL
var serverUrl = Xrm.Page.context.getServerUrl();
//The OData end-point
var ODATA_ENDPOINT = “/XRMServices/2011/OrganizationData.svc”;
//Asynchronous AJAX function to Delete a CRM record using OData
$.ajax({
type: “POST”,
contentType: “application/json; charset=utf-8”,
datatype: “json”,
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 DELETE to perform a delete operation.
XMLHttpRequest.setRequestHeader(“X-HTTP-Method”, “DELETE”);
},
success: function (data, textStatus, XmlHttpRequest) {
alert(“Record deleted successfully!!!!”);
},
error: function (XmlHttpRequest, textStatus, errorThrown) {
alert(“Error while deletion – “+errorThrown);
}
});
}
How Do I call this method :-
- To delete a “Account” record set account id & Odata set name and call the above method
var accountId = {}; //Set Account GUID
var odataSeName = “AccountSet”;
deleteRecord(accountId, odataSeName);
Hope it helps 🙂
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 🙂