web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

How can I create a dependent option set using Javascript in Dynamics 365 v8.2 on-premise?

(0) ShareShare
ReportReport
Posted on by

Hi everyone,

Our use case is we have two option sets. Both can be modified by the user. Optionset1 contains two values. Optionset2 contains a dozen values. We want Optionset2 to show values depending on what value is selected for Optionset1.

When no value is selected for Optionset1, we want Optionset2 to show no values (or be disabled).

We want the user to be able to change values for Optionset1 multiple times during the same session if necessary and always get the corresponding values for Optionset2.

We have individuals using the platform in French as well as English so the values should show in the language selected.

I have done some research on the web for the past few days. I found some javascript function that work partially but there's always a limitation. There seems to be a way to enumerate all the values in both languages upfront and assign them to variables onLoad of the form but this seems like an awful lot of code for such a basic functionality. What would be the leanest way to accomplish this?

Thanks in advance!

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    Community Member Profile Picture
    on at

    Hi Stephane,

    I would suggest that you stick close to what Microsoft is offering. This link https://docs.microsoft.com/en-us/previous-versions/dynamicscrm-2016/developers-guide/gg594433(v=crm.8) will most probably help you solve your problem completely, they use JS behind the scenes but you don't need to write it yourself.. If you are not satisfied with their approach, you can switch those option sets to lockups (by creating a custom entity) which has built-in cascading using some configuration. 

    This is another discussion that solves the issue as well 

    https://community.dynamics.com/crm/f/microsoft-dynamics-crm-forum/238567/cascading-option-sets

    Thanks!

  • Suggested answer
    PabloCRP Profile Picture
    1,088 on at
    Hi, hope it helps.
    /* Function to enable on formLoad and also on Optionset1 OnChange Event
    context: pass the form context as first parameter */
    var new_optionset2_globalOptions;
    function validateOptionSet(context){
    var formContext = context.getFormContext();
    var new_optionset1 = formContext.getAttribute("new_optionset1");
    var new_optionset2 = formContext.getAttribute("new_optionset2");
    if(new_optionset2_globalOptions===undefined)
    new_optionset2_globalOptions = new_optionset2.getOptions();
    var options1notAllowed = [71000000,71000001,71000002,71000003]; //Set your options (Number) not allowed for option 1
    var options2notAllowed = [71000005,71000006,71000007,71000008]; //Set your options (Number) not allowed for option 2
    if(new_optionset1.getValue()===71000000)//values of optionset1
    removeOptions(formContext,options1notAllowed,new_optionset2_globalOptions);
    else if(new_optionset1.getValue()===71000001)//values of optionset1
    removeOptions(formContext,options2notAllowed,new_optionset2_globalOptions);
    else
    removeOptions(formContext,null,new_optionset2_globalOptions);
    }

    /*Function to add and clear options
    formContext: pass the form context as first parameter
    optionsNotAllowed: values not allowed to be shown
    originalOptions : the global options of the optionset 2
    */
    function removeOptions(formContext,optionsNotAllowed, originalOptions){
    var attrLogicalName = "new_optionset2";
    var attr = formContext.getAttribute(attrLogicalName);
    var option;
    if(attr.getValue()!==null)
    option = attr.getOption(attr.getValue());
    formContext.getControl(attrLogicalName).clearOptions();
    if(optionsNotAllowed!==null){
    originalOptions.forEach(function(element){
    formContext.getControl(attrLogicalName).addOption(element);
    });
    optionsNotAllowed.forEach(function(element){
    formContext.getControl(attrLogicalName).removeOption(element);
    });
    }
    if(option!==undefined)
    attr.setValue(option.value);
    }
    /* Function to enable on Optionset1 OnChange Event
    context: pass the form context as first parameter */
    function clearoption2(context){
    var formContext = context.getFormContext();
    formContext.getAttribute("new_optionset2").setValue(null)
    }


Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans