Archive for June, 2013

Retrieve records with Fetchxml using Jscript – CRM 2011

June 20, 2013 1 comment

Retrieve the records using FetchXML is a convenient way as we can form the FetchXML easily using the ‘Advanced Find’ .

Download Fetch XML

Download Fetch XML

Once you have the FetchXML ready, below is the JScript to execute and read the result set.

  • In this sample, I am fetching records from custom entity “Bikes” which has relationship with ‘Contact’ entity. (i.e., Fetch all the bikes of a Contact whose full name contains ‘Raj’)
  • Note – I am using “xrmservicetoolkit” helper Jscript which is available in CodePlex. You just need to download the Jscript and refer to your CRM form.

function getBikesByContactName() {

var fetchXml =

“<fetch mapping=’logical’>” +

“<entity name=’raj_bike’>” +

“<attribute name=’raj_name’ />” +

“<attribute name=’createdon’ />” +

“<link-entity name=’contact’ from=’contactid’ to=’raj_customerid’ alias=’ad‘>” +

“<attribute name=’fullname’ />” +

“<filter type=’and’>” +

“<condition attribute=’fullname’ operator=’like’ value=’%raj%’ />” +

“</filter>” +

“</link-entity>” +

“</entity>” +


// Execute the fetch

var bikes = XrmServiceToolkit.Soap.Fetch(fetchXml);

// Get the results by loop through ‘bikes’

for (var indxBikes = 0; indxBikes < bikes.length; indxBikes++) {

alert(“Bike Name – “+bikes[indxBikes].attributes.raj_name.value);

alert(“Created On – “+bikes[indxBikes].attributes.createdon.value);

// Get the related entity (i.e., Contact) attributes using LinkEntity alias)

alert(“Related entity attribute (Contact Name) – ” + bikes[indxBikes];




Filtering Lookup view based on custom logic CRM 2011

June 17, 2013 1 comment

In one of my requirement, I have to filter my lookup view results based on another lookup field value.

I have 2 Lookup fields on my form

  • Product
  • Product Category

When I click on “Product” lookup, I have to show only Products with category as “Product Category” field value.

Filter Lookup View

Filter Lookup View

We can achieve this requirement using Jscript by following below steps

  • Create a new view with our filter
  • Use the SDK method addCustomView, to add newly created view for the lookup dialog box
  • Set this view as default view
  • Disable the view selector to avoid user to select other views from Lookup dialog
  • Put above logic in a function register it on “onchange” event of “Product Category” field

Below is the Jscript function

function createAndSetLookupView() {

// Some GUID but only needs to be unique among the other available views for the lookup

var viewId = “{CB6F8184-D7C2-4664-9FAB-18FD9DCDB22A}”;

// Name of the view

var viewDisplayName = “My filtered view”;

// Prepare the xml with columns to display and filter

// Tip : Use Advanced Find to prepare th FetchXML

var fetchXml = “<fetch version=’1.0′ ” +

“output-format=’xml-platform’ ” +

“mapping=’logical’>” +

“<entity name=’new_product’>” +

“<attribute name=’new_productid’ />” +

“<attribute name=’new_name’ />” +

“<attribute name=’new_categoryid’ />” +

“<attribute name=’createdon’ />” +

“<order attribute=’new_name’ ” +

“descending=’false’ />” +

“<filter type=’and’>” +

“<condition attribute=’new_categoryid’ ” +

“operator=’eq’ ” +

“uiname='” + {categoryName} + “‘ ” +

“uitype=’new_category’ ” +

“value='” + {categoryGUID} + “‘ />” +

“</filter>” +

“</entity>” +


var layoutXml = “<grid name=’resultset’ ” +

“object=’1′ ” +

“jump=’new_name’ ” +

“select=’1′ ” +

“icon=’1′ ” +

“preview=’2′>” +

“<row name=’result’ ” +

“id=’new_productid’>” +

“<cell name=’new_name’ ” +

“width=’150′ />” +

“<cell name=’new_categoryid’ ” +

“width=’150′ />” +

“<cell name=’createdon’ ” +

“width=’100′ />” +

“</row>” +


try {

// Lookup field which you want to add new view

var lookupControl = Xrm.Page.ui.controls.get(“new_productid”);

// Add the created view to Product lookup and set this view as default

lookupControl.addCustomView(viewId, “new_product”, viewDisplayName, fetchXml, layoutXml, true);

//Disable “View Selection” dropdown of “Product” lookup dialog

document.getElementById(“new_productid”).setAttribute(“disableViewPicker”, “1”);


catch (e) {

alert(“Error in createAndSetLookupView() – ” + e.description);




The server was unable to process the request due to an internal error – plugin registration tool

June 13, 2013 1 comment

The other day my plugin registration tool suddenly stopped connecting to my CRM server and was getting below error

Unhandled Exception: System.ServiceModel.FaultException: The server was unable to process the request due to an internal error

Below steps helped me to resolve the issue

  • Go to “Plugin Registration Tool” physical folder
    • Delete existing “Microsoft.Crm.Sdk.Proxy.dll” and “Microsoft.Xrm.Sdk.dll”

    Plugin registration tool folder

    • Get back the 2 .dlls either from CRM SDK or from “C:\Program Files\Microsoft Dynamics CRM\Server\bin” path of CRM server machine
    • Paste them in “Plugin Registration Tool” folder
  • Reset the IIS
  • Open the Plugin Registration Tool and try to connect to the server


Retrieve associated records (N:N related)

In CRM, If you create N:N relationship between 2 entities, it creates an intermediate entity (i.e., Relationship Entity) with 3 fields

  • Primary key field of “Relationship Entity”
  • Entity 1 Primary Key field
  • Entity 2 Primary Key field
N toN Relationship

N to N Relationship

In this sample, I have 2 custom entities “Bike” and “Bond” with N:N association. (i.e., A ‘Bike’ can have N no of ‘Bonds’ and a ‘Bond’ can be associate with N ‘Bikes’)

To get all the ‘Bonds’ of ‘Bike’ with name “Honda”, below is the query expression.

string entity1 = “raj_bike”;

string entity2 = “raj_bond”;

string relationshipEntityName = “raj_bike_bond”;

QueryExpression query = new QueryExpression(entity1);

query.ColumnSet = new ColumnSet(true);

LinkEntity linkEntity1 = new LinkEntity(entity1, relationshipEntityName, “raj_bikeid”, “{Entity 1 Primary key field (i.e.,raj_bikeid)}“, JoinOperator.Inner);

LinkEntity linkEntity2 = new LinkEntity(relationshipEntityName, entity2, “raj_bondid”, “{Entity 2 Primary key field (i.e.,raj_bondid)}“, JoinOperator.Inner);



// Add condition to match the Bike name with “Honda”

linkEntity2.LinkCriteria = new FilterExpression();

linkEntity2.LinkCriteria.AddCondition(new ConditionExpression(“raj_bikename”, ConditionOperator.Equal, “Honda”));

EntityCollection collRecords = service.RetrieveMultiple(query);

Note : To retrieve columns from Entity 2, set ‘Alias’ to ‘LinkEntity2’.