Diggin’ On James Brown…
Last month I posted a sample solution which demonstrated how to use some of the capabilities of CRM 2011 (such as JScript libraries, global option sets and the new client-side object model) to develop re-usable, dependent option sets (pick-lists). In that example I had two pick-lists on a form, one containing a list of countries and the other a list of regions (EMEA, APAC, Americas etc.). Whenever a country was selected from the first pick-list, a JScript function would display the correct region in the second pick-list.
But what if I wanted the reverse scenario, where I select a region from one pick-list, and only those countries from that region are visible in the second pick-list?
Like before, the code to achieve the simple case is fairly easy to write, but what if I wanted to build a JScript library that would allow me to re-use my logic, without having to hard-code specific field names? In addition, CRM 2011 introduces the ability for multiple copies of the same field on a single form, so how would my code cope with deal with a field which had multiple controls?
Luckily, CRM 2011 introduces a much richer client programming model than previous versions of CRM, which makes this straightforward. I have two main JScript functions, formOnLoad() and attributeOnChange(), which do some simple error handling before calling updateDependentOptionSet(), which does most of the heavy lifting.
Using the same option sets from the previous example, all I had to do was to add the country and region fields to a form, and wire up both the form OnLoad() and region field OnChange() events as shown below.
As before, I have selected the “Pass execution context as first parameter” checkbox, and specified the name of the relevant fields (in quotes). If you want to try this out on your own CRM 2011 environment, I have exported my solution as an unmanaged package, which you can download here.
Please note, there is a bug in CRM 2011 Beta (build 05.00.9585.101) which affects the behaviour of the Xrm.Page.ui.control.clearOptions() method, so this solution will only work if you are using CRM 2011 Release Candidate (build 05.00.9688.34).
This posting is provided "AS IS" with no warranties, and confers no rights.