{Dynamics CRM + Customer Hierarchy} How to show all related contacts in entire customer hierarchy in Dynamics CRM 2015 onwards

Recently my friend called up for a requirement where they needed to show all contacts in customer hierarchy. Let me explain the scenario here before moving further

Suppose Account A is parent customer for Account B and Account B is the parent customer for Account C.

So the requirement is when Account A form is loaded, a sub-grid should on the form should show all the contacts of Account A + Account B + Account C. If Account B is loaded then the sub-grid should show all the contacts of Account B + Account C.

Let us approach the solution here. Ideally it is a two step solution.

Step 1 – Prepare the fetchxml which would allow us to fetch all the contacts in the entire customer hierarchy.

Step 2 – Bind the fetchxml to the sub-grid at run time to show the contacts.

Now for the first part, CRM 2015 introduces the new ‘under’ operator which eases out our task like anything. After some discussion, my friend came up with a perfect fetchxml to achieve this. For e.g the following is the fetchxml to show all the contacts in the hierarchy of ‘Account A’.



Wonderful isn’t it. It just gives you the results you want for this. With 2013 and prior versions you can imagine how much you would need to code to achieve the same.

So we have the fetchxml. Now coming to step 2 which is to bind the fetchxml dynamically to the grid.

For 2015, there is already a wonderful blog which explains this – https://joshimandarblog.wordpress.com/2015/02/04/crm-2015-applying-custom-fetchxml-to-a-subgrid-using-javascript/

As much as we learn new concepts, I think the best thing is to see them in action in real life and my genius friend hit the bulls eye on this. He is a very well know face in Dynamics CRM – Nishant Rana.

This above logic would work not only for customer – contact relationship but in other areas of hierarchy as well like finding all users in a reporting hierarchy.

Hope this helps!