Without question, Dynamics 365 / Power Platform developers should always be leveraging Business Rules as opposed to implementing client-side scripting, using JavaScript or TypeScript. I’ve gone into detail previously on why this is such a crucial approach to follow, and many of these justifications do (in my humble opinion) stand the test of time well. To summarise, any solution built on top of Dynamics 365 or the Power Platform becomes far more maintainable when you are utilising functional solutions as opposed to ones written in code. This argument becomes even more potent when it is blindingly obvious that a non-technical approach will meet your particular requirements. The sign of an excellent technical developer is in recognising and fully utilising functional tools to the best of your ability, to the extent that you are actively minimising the amount of code you write. Microsoft seems to agree with this approach, which I think is part of the reason why they have weighted the specification for exam MB-400 (AKA the developer’s exam) to include areas typically reserved for a functional specialist of the application. Therefore, you should always, as a budding or experienced Dynamics 365 / Power Platform developer, keep yourself fully abreast and utilise all functional aspects of the platform where-ever possible.
With all this said and done, there are still occasions where we must, unfortunately, fall back to using JavaScript form functions. This state of affairs can become borderline maddening when you make a naive assumption that a Business Rule can do something but, upon closer inspection, this is not the case. Consider the following scenario: we have a multi-select option set field defined for our Account entity, whose properties resemble the below:
So far, so good. Let’s now assume we want to auto-hide this field on all of our Account forms, based on a pre-defined condition. We go to create our Business Rule, define our condition and then add on a Set Visibility action. Now we need to look at selecting the field above within the supplied drop-down box; however, in doing so, we notice a glaring omission:
The multi-select Option Set field - and indeed, any attribute of this type - is not supported for use alongside Business Rules. A quick Publish all customizations confirms that this is not a quirk of the application, but rather a specific limitation with Business Rules. Furthermore, fields of this type cannot be evaluated as part of a Business Rule condition or used within any other available action. How strange!
Fortunately, where there’s a will and a slight knowledge of coding, there’s a way. Because we have successfully exhausted the capabilities within Business Rule, we can now safely put together the following JavaScript form function:
function toggleMultiSelectVisibility (executionContext) {
//Currently, it is not possible to use Multi-Select Option fields alongside Business Rules
//This function, therefore, is used to show/hide the 'My Multi-Select Option Set' field
var formContext = executionContext.getFormContext(); // get formContext
/*
The function hides the field in all occassions. It may be necessary to add in an
if...else statement to get the functionality you desire
e.g.
if (formContext.getAttribute('myfieldname').getValue() === "value" {
formContext.getControl("jjg_testmultiselectoptionset").setVisible(false);
}
else {
formContext.getControl("jjg_testmultiselectoptionset").setVisible(true);
}
When accessing/setting multi-select option set fields, they are stored as an array of the
underlying integer values selected or to be set
e.g. [1, 2, 3, 4]
*/
formContext.getControl("jjg_testmultiselectoptionset").setVisible(false);
}
No doubt, you will need to tweak this form function to suit your particular requirements. Once ready, add it onto your form as part of the OnLoad or OnChange event of a specific field, and you’ll be able to hide the field successfully - hooray!
It is rather frustrating that, for a requirement that you think a Business Rule would address, we are let down and have to resort to writing code. Multi-select option set fields are relatively new in the history of Dynamics CRM / Dynamics 365, which might explain why Microsoft have not yet added them into Business Rules as a supported component. At the moment, there is an open idea for this feature, which I would urge you to vote for as well if you think having this functionality would be useful. For now, though, we can be reasonably content that the amount of code involved is minimal and that, most importantly, we have fully exhausted the native capabilities of Dynamics 365 / the Power Platform before even contemplating writing a single line of code 🙂