Archive

Posts Tagged ‘Delete record’

Delete record synchronously using OData and JScript in CRM 2011

April 25, 2012 1 comment

Hi,

Below is the script to delete record synchronously

function deleteRecordSync(recordId, odataSetName) {

// Get Server URL

var serverUrl = Xrm.Page.context.getServerUrl();

//The OData end-point

var ODATA_ENDPOINT = “/XRMServices/2011/OrganizationData.svc”;

var deleteRecordReq = new XMLHttpRequest();

var ODataPath = serverUrl + ODATA_ENDPOINT;

deleteRecordReq.open(‘POST’, ODataPath + “/” + odataSetName + “(guid'” + recordId + “‘)”, false);

deleteRecordReq.setRequestHeader(“Accept”, “application/json”);

deleteRecordReq.setRequestHeader(“Content-Type”, “application/json; charset=utf-8”);

deleteRecordReq.setRequestHeader(“X-HTTP-Method”, “DELETE“);

deleteRecordReq.send(null);

}

How do I call this method :-

  • To delete an “Account”
  • Pass account GUID and OData Set name (i.e.,AccountSet)

Var accountId=”{Account GUID}”;

deleteRecordSync (accountId , “AccountSet”);

🙂

Deleting a record using OData & JQuery in CRM 2011

February 20, 2012 Leave a comment

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 🙂