With the introduction of Option Sets in CRM 2011 (formerly known as picklists), it is now possible to create common drop-down lists across multiple entities and forms. On a recent project I had a specific requirement to build a solution that used two related option sets. If I selected a particular country from the first drop-down, the region would automatically selected from the second drop-down.
That doesn’t sound too tricky, and indeed writing a small amount of JScript I was able to achieve this fairly quickly. The problem came about when I wanted to use these option sets multiple times on a single entity. In this example, I needed to track “Country of Birth”, “Country of Residence” and “Tax Residency” as well as the associated regions. Now I have three variations of the same JScript code, with the only difference being the CRM schema names of the various country and region fields.
This got me thinking. Could I build a JScript library that would allow me to reuse the dependent option set logic, without having to hard-code specific field names? A quick search through the CRM 2011 SDK revealed several major enhancements to the client-side programming model, and of specific interest to me were:
- The ability to access the form execution context. When you associate a JScript library function with a CRM form event (such as a field OnChange event), you can now pass the execution context as a parameter to your function. The execution context object has a method called getEventSource(), which returns a reference to the field that triggered the event. In this example, I no longer have to hard-code the name of the country field that triggered the OnChange event.
- The ability to pass additional parameters to a JScript library function. In this example I specify the name of the region field as an additional parameter, instead of using a hard-coded value in my code.
So what does my code look like? Below, I have a simple JScript function called attributeOnChange(), which takes the both execution context and the dependent attribute name as input parameters. After some some simple error handling, the function then calls a second function getDependentAttributeValue(), which actually does the work of selecting the correct region, based on which country was selected. I’ve chosen to use the names attribute and dependentAttribute instead of country and region, so I can reuse this code for any dependent option sets.
So now I have the necessary option sets and JScript library to solve my original problem, all that’s left is to add the fields to a form, and wire-up the OnChange() event as shown.
As you can see, I have selected the “Pass execution context as first parameter” checkbox, and specified the name of the region field “srh_region”, not forgetting to put quotes around the name. If you want to try this out in your own CRM 2011 environment, I have exported my solution as an unmanaged package, which you may download here.
This posting is provided “AS IS” with no warranties, and confers no rights.