Gain More Flexibility with Collection Functions
Often times we run into trouble when using collection functions with vocabulary expressions. Part of the problem is that when we want to use our named placeholders for the criteria of those expressions, we do not have access to the fields and vocabulary expressions at the child level. In this post, we are going to use a relatively simple technique to give you more flexibility when using collection functions in your vocabulary expressions. It is important to note that this technique applies primarily for Business Language rules.
Let us say we have a top-level entity and we want to compare the number of members in a child collection against the total number of members. If we were to use the standard “the number of” business language template, when we use the filter criteria we can see the child level fields without issue.
But what happens if we want to have a vocabulary template with a placeholder for my filter criteria for an aggregate function, like so?
What you will see is that you do not have access to the child level fields and vocabulary. If you choose “Borrower” under “Collection Entities” it will ask for the index. If you choose “Borrowers” then it would have the option of selecting “is null” or “is not null.”
What we can do is create a simple vocabulary expression template that returns the context of the child entity. To do this you just need to select the appropriate entity type in the “Template Type” and put the target name under Expression like so:
Then when using your original vocabulary expression, you simply first use your new context shifting template. Afterwards, you will have access to all the fields at that child context as well as vocabulary expression at this context. I have illustrated these steps below:
This now allows us to complete the condition and upon testing, it works.
With that simple expression template, we now have access to fields and vocabulary in the context of the child collection. It is important to note that if you try to use this technique for a filter in a grandchild collection, you will again have errors with the context. However, if you keep it at a parent/child level you can now use place holders for filter criteria for aggregate functions and collection lookups for a more robust rule authoring experience.