Cross-browser JavaScript functions to Retrieve and Update CRM Records

There’re plenty of examples on the Internet of how to use JavaScript to retrieve and update records in CRM 2011 or CRM 2013. However, I find many of them are overly too complex and require inclusion of proprietary JavaScript libraries, some doing them asynchronously, and some don’t work with non-IE web browsers.

Here’s a version that I use, taken from various sources (sorry, I lost track of the original sources as I’ve looked at so many!). It only requires inclusion of JSON2, and consists of one method for retrieve, and another method for update.

RetrieveRecords

function RetrieveRecords(query) {
 var odataUri = Xrm.Page.context.getServerUrl() + "/XRMServices/2011/OrganizationData.svc/" + query;
 var service;

 if (window.XMLHttpRequest) {
  service = new XMLHttpRequest();
 } else if (window.ActiveXObject) {
  service = new ActiveXObject("Microsoft.XMLHTTP");
 } else {
  Xrm.Utility.alertDialog("Unable to initiate service to execute query on this client platform.");
  return null;
 }

 service.open("GET", odataUri, false);
 service.setRequestHeader("X-Requested-Width", "XMLHttpRequest");
 service.setRequestHeader("Accept", "application/json,text/javascript, */*");
 service.send(null);

 var requestResults = eval('(' + service.responseText + ')').d;
 return requestResults;
}

UpdateRecords


function UpdateRecord(id, entityObject, odataSetName) {
 var jsonEntity = window.JSON.stringify(entityObject);
 var odataUri = Xrm.Page.context.getServerUrl() + "/XRMServices/2011/OrganizationData.svc";
 var service;

 if (window.XMLHttpRequest) {
  service = new XMLHttpRequest();
 } else if (window.ActiveXObject) {
  service = new ActiveXObject("Microsoft.XMLHTTP");
 } else {
  Xrm.Utility.alertDialog("Unable to initiate service to execute query on this client platform.");
  return null;
 }

 service.open('POST', odataUri + "/" + odataSetName + "(guid'" + id + "')", false);
 service.setRequestHeader("Accept", "application/json");
 service.setRequestHeader("Content-Type", "application/json; charset=utf-8");
 service.setRequestHeader("X-HTTP-Method", "MERGE");
 service.send(jsonEntity);
}

Usage examples

// Get latest Opportunity record by the current user
var userId = Xrm.Page.context.getUserId();
var query = "OpportunitySet?$select=CreatedOn,OpportunityId,Name&$filter=CreatedBy/Id eq (guid'" + userId + "')&$orderby=CreatedOn desc";
var items = RetrieveRecords(query);
var opp = items.results[0];

// Update the User record with the latest Opportunity reference
var user = new Object();
user.new_LatestOpportunityId = { Id: opp["OpportunityId"], LogicalName: "opportunity", Name: opp["Name"] };
UpdateRecord(userId, user, "SystemUserSet");
Advertisements

One thought on “Cross-browser JavaScript functions to Retrieve and Update CRM Records

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s