Today, I won’t be going into a detailed explanation of how to dynamically change the fetchXML for a sub-grid. Instead, I’m going to show you a small detail that will fix your view. So let’s begin…

We have the following structure:

Account –> Account Products (One to Many)

Product–> Account Product (One to Many)

Target (Opportunity) –> Account Products (One to Many)

In short, Account Product looks up to Account, Product and Target (Opportunity).

The Active Account Products View will show some fields from Account Products, some fields from Product and some fields from Target. However, many people bump into some fetchXML restrictions when building views. The solution is to add a sub-grid for Product Account on the Account form and dynamically change the fetchXML to a sequence of queries, through JavaScript.

 

 

Then add the code to change the query for the sub-grid:

 

Then bind the function to the OnLoad event for the form, or to the TabStateChange event for the tab that holds the sub-grid.

And then test it out:

 

But … Oops … the fields from the related entities are empty!

So there it is … the problem is in the aliases for the linked entity in the fetchXML. The view/grid layout refers to the related entities fields through the aliases defined in the fetchXML. And of course the new custom fetchXML used to replace the original fetchXML doesn’t have exactly the same aliases for the linked entities. The trick is to open up the original view:

 

And check out the fetchXML:

 

Nice! Copy over those GUIDs for the aliases in your new custom fetchXML for the sub-grid:

 

And test again:

 

So the twist (fix), from the title is: use the same aliases from the original fetchXML of that view that you are replacing in the sub-grid.