Advertisements

Archive

Archive for April 25, 2012

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”);

🙂

Advertisements

Update record synchronously using OData and JScript in CRM 2011

Hi,

Below is the Jscript function to update record synchronously using OData and Jscript

function updateRecordSync(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”;

var updateRecordReq = new XMLHttpRequest();

var ODataPath = serverUrl + ODATA_ENDPOINT;

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

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

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

updateRecordReq.setRequestHeader(“X-HTTP-Method”, “MERGE“);

updateRecordReq.send(jsonEntity);

}

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
  • Get the helper files from CRM 2011 SDk under path “\sdk\samplecode\js\restendpoint\jqueryrestdataoperations\jqueryrestdataoperations\scripts”
  • To update an “Account”
  • Pass “Account” object and GUID and OData Set name (i.e.,AccountSet)

Var accountId=”{Account GUID}”;

var account = new Object();

account.Name = “Rajeev Pentyala”;

updateRecordSync (accountId ,account, “AccountSet”);

🙂