FetchXML Trick – Cross Entity Criteria in a Group Filter
Recently, I had a tricky question: Can you create a view that is displaying records that are either owned by a team or the users that are a member of that particular team.
The TL;DR; Answer
Use “Alias” + FetchXML editing using XrmToolBox (View Designer + FetchXML Builder).
The Longer Answer
The typical answer to the above scenario would be querying the record with the criteria as “Owner ID = the team OR related Owner (user) is a team member of the team”. Using the Advanced Find query, typically this is the query…
However, it seems we can’t group the “main” criteria and the Linked-Entity criteria. Looks like we are stuck here… Worry not, there is a trick for that. So, before I go to the solution here are some setups to set the background.
I have 3 users in my instance:
And I’ve set 2 teams for the testing purpose:
Team A with myself and Test User 1 as the member.
And Team B with Test User 2 as the member.
Now I have some records for testing purpose:
So, when based on the query to see the Team A records, I would expect to see 3 records there. Using the initial query (above) will return no result, as it is treated as ‘AND’ operation.
To get the FetchXML correctly, I need to edit the FetchXML, my go-to tools for this would be XrmToolBox‘s FetchXML Builder.
To start with, I’ll export the FetchXML out of my initial query, it will look like this:
Now the trick is to use the “alias” of the related team entity to form the FetchXML like this:
As you can see above, we are able to set the condition in the “main” filter and set the operator to OR. Below is the result of the fetch… it works as expected
Now, I want to put the query to the view, how to do that?
I’ll use XrmToolBox’s View Designer to modify the FetchXML of the view. In my case, I’ve setup a Test Query view. Click on the Edit Query, this will open up the FetchXML builder.
Copy & Paste the FetchXML that we had tested previously into the FetchXML window, if you wish, you could test execute the query. Then click on the “Return FetchXML” button up there to go back to the View Designer.
Then click on Save and then Publish..
And here’s the result:
HTH!
This was originally posted here.
*This post is locked for comments