“I have Vehicle custom entity in Dynamics. How do I find all Vehicles that belong to a particular Dynamics business unit?”
Heh.. Strange you are asking – just go to the Advanced Find and build a filter – it’s very simple, isn’t it?
Only it’s not, as I just learned.
If you look at any of the user-owned entities (we are not talking about the organization-owned entities), you will see four attributes related to this question:
The way they work is:
- OwnerId is, essentially, a complex field. It can be a team reference, or it can be a user reference
- When a record is assigned to a team (OwnerId is a team then), owningteam attribute is updated to reference the same team. Owninguser attribute is set to null
- When a record is assigned to a user (OwnerId is a user then), owninguser attribute is updated to reference the same user. Owningteam attribute is set to null
- In either case, owningbusinessunit attribute is set to the business unit of the owner (which is either a team business unit or a user business unit)
So far so good. But this is where it takes an unexpected turn.
How would you actually build a filter in the Advanced Find to answer the question above?
Can we use Owning Business Unit to define the filtering criteria?
Have a look at the screenshot:
As you can see, there is no OwningBusinessUnit there. Why would it not be there? That was the question I had to ask myself earlier today. The answer, as it turned out, is simple.. OwningBusinessUnit field is not searchable:
And we can’t really change that setting. Well, this starts looking like a very special field. However, there are other special fields, so it’s not quite unusual to find another one. What is unusual is that for some of the other entities OwningBusinessUnit IS searchable. For example, it totally works for the Contact entity:
This is where my initial confusion came from. I did remember using Owning Business Unit field in the Advanced Find. I just did not realize I was likely using it with one of the out-of-the-box entities, and not with a custom entity.
However, what it means is that we actually cannot create a filter that will be using OwningBusinessUnit attribute in the Advanced Find.
So what if we try something else..
Can we define correct filter using some combination of the OwningTeam and OwningUser attrbutes?
Well, we could try something like this:
But that would not help at all since we would be asking Dynamics to return all records which are owned by a team in business unit 2 AND by a user in business unit 2. There can be only one owner, so this would not work.
So, how about making it an “OR” condition? Let’s try Group OR?
This is where I ran into another problem:
We can’t use Group OR unless all those attributes are listed under the same entity.
Are you starting to see the problem?
What if we tried to define the filter differently?
If the owning team is not in any other BU and the owning user is not in any other BU, then one of them must be in the correct BU, right? So let’s try it:
Problem is, again, this actually translates into a fetch query that is looking for an owning team AND for an owning user.
And, then, we are suddenly out of options.
What’s left is:
- We can build two different views (records owned by the individual users, and, also, records owned by the teams)
- We can start customizing: add a new business unit lookup field, create a workflow/plugin to populate that field, then use it for the filters
Finally, there can be more advanced customizations:
- we might build a pre-retrievemultipe plugin to add a condition for the owningbusinessunit
- we might probably use XmlToolBox/SDK to update the view without utilizing the Advanced Find
With the FetchXml, this kind of fetch works fine even if I can’t get it directly from the AdvancedFind:
Either way, I don’t really know the reason why owningbusinessunit is not searchable for custom entities. Maybe it was implemented that way for the performance reasons, I’m not sure. It’s probably going to stay a mystery – there are a few of those in Dynamics.