Saturday, March 19, 2011

CRM 2011 Retrieve Request - Retrieve Entity Record from Related Entity

With the new release of MS CRM 2011, there are new and better ways to peform some client side scripting.

Below is an example on how to retrieve data from the account entity on the contact form.



function RetrieveAccount(oAccount)
{
var oReq = getXMLHttpRequest();
if (oReq != null)
{
if (oAccount[0] != null)
{
oReq.open("GET", Xrm.Page.context.getServerUrl() +"/XRMServices/2011/OrganizationData.svc/AccountSet?$filter=AccountId%20eq%20(guid'" + oAccount[0].id + "')&$select=Name,AccountId,Telephone1", true);

oReq.onreadystatechange = function () { DisplayAccountData(oReq) };
oReq.send();
}
}
else
{
alert('not supported');
}
}

function getXMLHttpRequest () {
if (window.XMLHttpRequest) {
return new window.XMLHttpRequest;
}
else {
try
{ return new ActiveXObject("MSXML2.XMLHTTP.3.0"); }
catch (ex) {
return null;
}
}
}

function DisplayAccountData (oReq)
{
if (oReq.readyState == 4 /* complete */)
{
if (oReq.status == 200)
{
xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async = false;
xmlDoc.loadXML(oReq.responseText);
alert('Name: ' + xmlDoc.selectSingleNode("//d:Name").text);
alert('Guid: ' + xmlDoc.selectSingleNode("//d:AccountId").text);
alert('Phone: ' + xmlDoc.selectSingleNode("//d:Telephone1").text);
}
}
}



The first function receives account object then creates an XmlHttpRequest. Next is the retrieve method:
oReq.open("GET", Xrm.Page.context.getServerUrl() +"/XRMServices/2011/OrganizationData.svc/AccountSet?$filter=AccountId%20eq%20(guid'" + oAccount[0].id + "')&$select=Name,AccountId,Telephone1", true);
This code uses the "AccountSet" and the query string keys filter and select. We retrieve using the AccountId and return the attributes Name, AccountId, and Telephone1. A SQL query would look like this SELECT Name, AccountId, Telephone FROM Account WHERE AccountId = '[account id value here]'. You can also retrieve on other attributes like Name. Example: $filter=Name%20eq%20'XYZ Company'

The rest of the code waits for the form to load and then uses the returned xml to get the attributes and display each value in alerts.

Now to get it working.



  1. We need to create a javascript file that we will use to upload when creating our library. Copy the code above and save to a file called RetrieveAccount_jscript.js (you can name it differently if you prefer).


  2. We will need to create a library in the Web Resouce. Go to Settings >> Customizations >> Customize the System, then click on the Web Resources left nav link. Click New, which will open a window to enter the following:

    • Name: new_retrieverequest
    • Display Name: Retrieve Request
    • Description: Retrieve account information using the account object parameter.
    • Type: select Script (JScript)
    • Language: English
    • Upload File: RetrieveAccount_jscript.js
  3. Open the Conact Customizable Entity. Open the Main form. Click on Form Properties.

  4. Add the new_retrieverequest library.
  5. Below, the Library section, add the Event Handler with Control=Form and Event=OnLoad.
  6. Add the Event Handler by selecting the libary new_retrieverequest and enter the function name RetrieveAccount. Make sure Enabled is checked.
  7. Next we need to fill out the parameter section. Uncheck the pass context parameter. Enter the following as the parameter to pass to the function: crmForm.parentcustomerid.DataValue.

Click OK on the windows, Save the entity, and Publish. Now open a contact record that has a related parent (which contains a main number). You will receive 3 alert windows displaying the name, guid, and a phone number.


No comments:

Post a Comment