Dependent OptionSet in Dynamics CRM V9

It is known that for the moment there is no way to set up a dependency between two OptionSet fields with OOB functionalities. It is possible to implement this scenario using JavaScript code as the field type used is OptionSet. We could have implemented it with a simple configuration if the Field type were a LookUp.

Example:

Let’s take the following example:
– OptionSet A with the following values A1, A2.
– OptionSet B with the following values B1, B2, B3, B4, B5, B6.
– If A1 is selected, only B1, B2, B4 can be selected.
– If A2 is selected, only B3, B5, B6 can be selected.

I implement a JavaScript method that you can use for every two dependents OptionSet fields:

if(typeof(MEA)=="undefined"){MEA={}};
if(typeof(MEA.OptionSet)=="undefined"){MEA.OptionSet={}};
if(typeof(MEA.OptionSet.Utilities)=="undefined"){MEA.OptionSet.Utilities={}};
MEA.OptionSet.Utilities = {
//Cascading Utility
optionSetBValues : null,
Cascade: function (executionContext, optionAName, optionBName, dependecies) {
var formContext = executionContext.getFormContext();
var selectedAValue = formContext.getAttribute(optionAName).getValue();
var optionSetBControl = formContext.getControl(optionBName);
if (optionSetBValues == null)
optionSetBValues = optionSetBControl.getOptions();
if (selectedAValue != null) {
optionSetBControl.clearOptions();
var dependeciesB = dependecies.find(d => d[0] == selectedAValue).slice(1);
var filtredOptionB = optionSetBValues.filter(v => dependeciesB.includes(v.value));
filtredOptionB.forEach(d => {
optionSetBControl.addOption(d);
})
}
else {
optionSetBControl.clearOptions();
}
}
}


Comma separated list of parameters: 

“mea_optionacode”,”mea_optionbcode”,[[1,1,2,4],[2,3,5,6]]

  • “mea_optionacode”: optionA schemaName.
  • “mea_optionbcode”: optionB schemaName.
  • [[1,1,2,4],[2,3,5,6]]: Array containing the dependencies:
    • [1, 1, 2, 4]: value 1 selected, only values 1, 2, 4 can be selected.
    • [2, 3, 5, 6]: value 2 selected, only values 3, 5, 6 can be selected.

6 thoughts on “Dependent OptionSet in Dynamics CRM V9

  1. Hi Mehdi, where do you enter the code for option A & B in the above script? Can you showcase the full details in the script based on your sample option set “mea_optionacode”,”mea_optionbcode”,[[1,1,2,4],[2,3,5,6]]?

    Thanks.

    Like

    1. Hi,

      The code I provided is generic, it works for any two optionsets.

      The first two parameters are the schemaName of the two optionSets. The third parameter is an array that represents the dependencies between the values of the two optionSets.

      Like

  2. Hi Mehdi,

    I don’t have knowledge in JS, I have a Product Type & Product Subtype where product subtype is dependent of product type. What should I input under the MEA field?

    And is it only the text with optionAName & optionBName that I need to replace with my Product Type & Product Subtype?

    Thanks.

    Like

    1. Hi,

      Yes, you need to replace optionAName & optionBName with the SchemaName of your two fields. Also, you need to configure the dependencies with the third parameter.

      Don’t hesitate to contact me via my following email address if you need help: meelamri23@gmail.com

      Like

      1. TypeError: executionContext.getFormContext is not a function

        I am receiving this error. Function on line number 9 (executionContext.getFormContext();) is not defined. Can you help, how to resolve this error?

        Like

Leave a comment