Sunday, 12 June 2011

Retrieve using ODATA and JSON in CRM 2011

This is a quick simple way to use OData Query. In this example we are using the code on the Contact entity to get data from Account enity. We are going to use OData Query to get data onchange of a lookup. 


When a user changes the Parent Account lookup they would like the contact to have the same address information as the Account. Should they move the contact to another account their address changes as soon as the lookup has changed.

I have put a function 'retrieve' on the onchange event of parentcustomerid on the Contact Form.


Make sure you add Json2.js and JQuery -1.5.mins.js to the Entity


The files can be found in the sdk at this location:
sdk\samplecode\js\restendpoint\restjquerycontacteditor\restjquerycontacteditor\scripts


Change the code below accordingly to any form you wish by following the instructions in highlighted green. 


Enjoy 


*********************************************************************
function retrieve() {
//1.Get Guid from lookup on the form, change lookup name accordingly
var lookupObject = Xrm.Page.getAttribute("parentcustomerid");
if (lookupObject != null)
{
var lookUpObjectValue = lookupObject.getValue();
if ((lookUpObjectValue != null))
{
var lookupid = lookUpObjectValue[0].id;
}
}




var id = lookupid;
 //2.Change the second parameter to the Entity you want i.e. "ContactSet"
 retrieveRecord(id, "AccountSet", retrieveCompleted, null);
}




function retrieveCompleted(data, textStatus, XmlHttpRequest) {
 //Example of how to assign the data to CRM fields
//3. Assign the data to your CRM fields
var account = data;
Xrm.Page.getAttribute("address1_line1").setValue(account.Address1_Line1);
Xrm.Page.getAttribute("address1_line2").setValue(account.Address1_Line2);
Xrm.Page.getAttribute("address1_line3").setValue(account.Address1_Line3);
Xrm.Page.getAttribute("address1_city").setValue(account.Name);
Xrm.Page.getAttribute("address1_postalcode").setValue(account.Address1_PostalCode);
Xrm.Page.getAttribute("address1_stateorprovince").setValue(account.Address1_StateOrProvince);
Xrm.Page.getAttribute("address1_country").setValue(account.Address1_Country);
}


function retrieveRecord(id, odataSetName, successCallback, errorCallback) {
//You don't need to edit anything in this function at all! If this function
//works it will return the data to function retrieveCompleted(above)
var context = Xrm.Page.context;
var serverUrl = context.getServerUrl();
var ODATA_ENDPOINT = "/XRMServices/2011/OrganizationData.svc";




 if (!id) {
  alert("record id is required.");
  return;
 }


 if (!odataSetName) {
  alert("odataSetName is required.");
  return;
 }


 //Asynchronous AJAX function to Retrieve a CRM record using OData
 $.ajax({
  type: "GET",
  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");
  },
  success: function (data, textStatus, XmlHttpRequest) {
   if (successCallback) {
    successCallback(data.d, textStatus, XmlHttpRequest);
 
   // Use this method for a selection that may return multiple entities
   //RetrieveMultipleEntities(data.d.results);
 
   //Example of what you might want in the RetrieveMultipleEntities function when you create it
   //var account = data.d.results[0];
   //var aname = eval('account.AccountNumber');
   //alert(aname);
 
   }
  },
  error: function (XmlHttpRequest, textStatus, errorThrown) {
   if (errorCallback)
    errorCallback(XmlHttpRequest, textStatus, errorThrown);
   else
    errorHandler(XmlHttpRequest, textStatus, errorThrown);
  }
 });
}

1 comment: