Search code examples
javascriptdynamics-crm-2011dynamics-crm-2016xrm

Filtering options on Business Process flow OptionSetValue step


I have a method that allows me to filter out certain options from an OptionSetValue field.

It works fine on a form field, but when that field is in the header, for a business process flow, it "works" as in, through debugging I see the options being cleared and re-added (only the ones that should be there), but once the form is rendered, all the options are visible...

Here's the method:

FilterOptionSetValues: function (fieldName, visibleOptions) {

            var ctrl = Xrm.Page.getControl(fieldName);
            var allOptions = ctrl.getOptions();

            //clear current options
            ctrl.clearOptions();

            //loop through all options of optionset and if one is found in config element, add it.
            for (var x = 0; x <= allOptions.length - 1; x++) {
                if (visibleOptions.availableOptions.indexOf(parseInt(allOptions[x].value)) > -1) {
                    ctrl.addOption(allOptions[x]);
                }
            }
}

And here's how I call it:

FilterOptionSetValues('header_process_new_my_optionset_field', { stage: 1, availableOptions: [300000002, 300000003, 300000004] });

This code is called either in the form load event and the OnChange event of another fields (salesstage).

Is there something I'm missing? Seems like MS's own javascript is undoing my work here...

EDIT: When I put an OnChange listener on header_process_new_my_optionset_field, nothing happens when I change the value of that field in the header business process flow, but an onChange listener on new_my_optionset_field will be triggered by a change on that field either on the form or the header business process flow.

But running the logic above only on the field new_my_optionset_field doesn't do the filtering for that same field up there in the business process flow.


Solution

  • By doing a console.log of the name of all the form's controls (Xrm.Page.ui.getControls().getAll()), I found that there is an instance of the control for that attribute on each stage of the process, followed by 1, 2, 3 and so on. The same field is present on all stage of the business flow.

    So I changed the code above for:

    var control = Xrm.Page.getControl(fieldName);
    var allOptions = control.getAttribute().getOptions();
    
    //clear current options
    control.clearOptions();
    
    //below, same as above...
    

    And called it for all as such:

    FilterOptionSetValues('header_process_new_my_optionset_field', { stage: 1, availableOptions: [300000002, 300000003, 300000004] });
    FilterOptionSetValues('header_process_new_my_optionset_field1', { stage: 1, availableOptions: [300000002, 300000003, 300000004] });
    FilterOptionSetValues('header_process_new_my_optionset_field2', { stage: 1, availableOptions: [300000002, 300000003, 300000004] });
    //and so on...
    

    It was working at first, but only filtering the options in the first stage of the flow, which wasn't the active stage when testing so it gave the impression of not working...