Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Dynamics 365 general forum

Dependent OptionSet Not Working in D365 Business Process Flow Field

(0) ShareShare
ReportReport
Posted on by 125

Hello,

We basically took the DependentOptionSet.js file from the Microsoft SDK and modified it to work on fields in the business process flow and not just on fields on a form. However, we are upgrading to D365 and this code dosent work on the field in the business process flow anymore. Has anybody else ran into this?

Below is the code snippit:

function subTypeInit(executionContext)
{
	var formContext = executionContext.getFormContext(); // get formContext
    var stageName = formContext.data.process.getActiveStage().getName();
    if (stageName == "somestage")
    {
        var parentField = "new_someparentfield";
        var childField = "new_somechildfield";
        formContext.getControl("header_process_new_someparentfield").getAttribute().addOnChange(setDepenetOptionSet(executionContext, parentField, childField));
    }
    else
    {
        return;
    }

}

function setDepenetOptionSet(executionContext, parentField, childField)
{
	var formContext = executionContext.getFormContext(); // get formContext
    for (var depOptionSet in SDK.DependentOptionSet.config)
    {
        var DependentOptionSet = SDK.DependentOptionSet.config[depOptionSet];
        /* Match the parameters to the correct dependent optionset mapping*/
        if ((DependentOptionSet.parent == parentField) && (DependentOptionSet.dependent == childField))
        {
            /* Get references to the related fields*/
            var ParentField = formContext.getControl("header_process_" + parentField).getAttribute(); 
            var ChildField = formContext.getControl("header_process_" + childField).getAttribute(); 
            /* Capture the current value of the child field*/
            var CurrentChildFieldValue = ChildField.getValue();
            /* If the parent field is null the Child field can be set to null */
            if (ParentField.getValue() == null)
            {
                ChildField.setValue(null);
                ChildField.setSubmitMode("always");
                ChildField.fireOnChange();

                // Any attribute may have any number of controls
                // So disable each instance
                var controls = ChildField.controls.get()

                for (var ctrl in controls)
                {
                    controls[ctrl].setDisabled(true);
                }
                return;
            }

            for (var os in DependentOptionSet.options)
            {
                var Options = DependentOptionSet.options[os];
                var optionsToShow = Options.showOptions;
                /* Find the Options that corresponds to the value of the parent field. */
                if (ParentField.getValue() == Options.value)
                {
                    var controls = ChildField.controls.get();
                    /*Enable the field and set the options*/
                    var runOnce = true;
                    for (var ctrl in controls)
                    {
                        if (!runOnce) break;
                        runOnce = false;
                        controls[ctrl].setDisabled(false);

                        //This is a workaround to get the child controls to refresh
                        //controls[ctrl].clearOptions();
                        var sub_category = formContext.ui.controls.get("header_process_" + childField);
                        sub_category.clearOptions();

                        for (var option in optionsToShow)
                        {
                            controls[ctrl].addOption(optionsToShow[option]);
                        }

                    }
                    /*Check whether the current value is valid*/
                    var bCurrentValueIsValid = false;
                    var ChildFieldOptions = optionsToShow;

                    for (var validOptionIndex in ChildFieldOptions)
                    {
                        var OptionDataValue = ChildFieldOptions[validOptionIndex].value;

                        if (CurrentChildFieldValue == OptionDataValue)
                        {
                            bCurrentValueIsValid = true;
                            break;
                        }
                    }
                    /*
                    If the value is valid, set it.
                    If not, set the child field to null
                    */
                    if (bCurrentValueIsValid)
                    {
                        ChildField.setValue(CurrentChildFieldValue);
                    }
                    else
                    {
                        ChildField.setValue(null);
                    }
                    ChildField.setSubmitMode("always");
                    ChildField.fireOnChange();
                    break;
                }
            }
        }
    }
}

5621.D365.jpg Behavior in D365

4061.D2015.jpg Behavior in Dynamics 2015

  • Verified answer
    Haris Lilic Profile Picture
    Haris Lilic 125 on at
    RE: Dependent OptionSet Not Working in D365 Business Process Flow Field

    So I figured it out by taking out the below code:

                       var runOnce = true; <---- Delete

                       for (var ctrl in controls)

                       {

                           if (!runOnce) break; <---- Delete

                           runOnce = false; <---- Delete

                           controls[ctrl].setDisabled(false);

    So in 2015 we only ran through this loop once since we found that it was hitting both the field in the BPF and the form on the first iteration, and then if it continued on it would double the options. Now in D365 it seems like they seperated the controls from the fields, so this current code would only work on the field on the form. Once I removed the runOnce logic, it worked fine for both the field in the form and in the BPF without doubling the options.

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

November Spotlight Star - Khushbu Rajvi

Congratulations to a top community star!

Forum Structure Changes Coming on 11/8!

In our never-ending quest to help the Dynamics 365 Community members get answers faster …

Dynamics 365 Community Platform update – Oct 28

Welcome to the next edition of the Community Platform Update. This is a status …

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,516 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,321 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans