Getting Related Record Lookup Fields with Retrieve Multiple Using The REST Endpoint With JavaScript in Dynamics CRM

There are multiple ways to get a data about related entity lookup in Dynamics CRM, like using SOAP or OData endpoint which are explained in SDK, but here I’m going to use a optimized JavaScript library called XrmServiceToolkit. You can get this library from CodePlex.

This library has built on JSON and JQuery and has multiple functionality that enable you to use SOAP or Rest or other functionality in one place.

In order to use this library download it from codeplex and add them to your Dynamics CRM form libraries.(json2, jquery, xrmservicetoolkit)

formlibrary

for instance I want to get some data about “Existing Product” lookup in “Opportunity Product”  entity, to do some calculation on the form.

In opportunity product form I add my library called “Opportunity_Product_JS” to use XRMServiceToolkit, RetriveMultiple method:

function calcQuantity() {
//getting id and name of the lookup record to use in our RetriveMultiple filter
var objProduct = Xrm.Page.getAttribute("productid");
if (objProduct != null) {

var objProductValue = objProduct.getValue();

if ((objProductValue != null)) {
//getting  name of the lookup
var lookuptextvalue = objProductValue[0].name;
//getting id of the lookup
var prodId = objProductValue[0].id;

if (prodId != null) {

var myResult = getLookup(prodId);
//do your calculation with myResult
}

}
}
}

//function will return the lookup data
function getLookup(prodId) {
 var res;
 XrmServiceToolkit.Rest.RetrieveMultiple(

  "ProductSet", 
  //you can get this from your OData endpoint in setting/developer resources. 
  //search for your entity name in XML. it follows this rule: entityname+set

  "$filter=ProductId eq guid'"; + prodId + "'",
  // filter will search for a product that is defined inside the lookup and will put it in the result

 function (results) {
 //Important Note:If you expect more than one page (50) of records
 // this function should loop through the results and push the records into an array outside of the function.

   var res = results; 
  //if your filter returns more than 50 records this statement will return incorrect results.Follow the link at the end of this post!
  },

  function (error) { alert(error.message);},
   function onComplete() {  },
   false
  );
return res;
}

Also check this blog post for more comprehensive overview of XrmServiceToolkit.Rest.RetrieveMultiple.

Advertisements

One thought on “Getting Related Record Lookup Fields with Retrieve Multiple Using The REST Endpoint With JavaScript in Dynamics CRM

  1. Pingback: Retrieve Multiple Records with XrmServiceToolkit | Ali Sharifi's Blog

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