0

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.

Francis Ducharme
  • 4,848
  • 6
  • 43
  • 81
  • Do you have an OnChange handler registered for the attribute and does it re-add the options? clearOptions() calls the onChange event handler. – dynamicallyCRM Aug 18 '16 at 20:47
  • @dynamicallyCRM I have edited my question and specified where the code gets called. – Francis Ducharme Aug 18 '16 at 20:49
  • Try to wrap the call in a timeout function and run it after 5 seconds or so to rule out CRM script asynchronously updating it after the form load? – dynamicallyCRM Aug 18 '16 at 20:56
  • @dynamicallyCRM Tried that and validated that the code only gets called when I expect it to be called. I will put an onChange listener on the header field and see what happens. – Francis Ducharme Aug 19 '16 at 12:57

2 Answers2

0

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...

Francis Ducharme
  • 4,848
  • 6
  • 43
  • 81
0

Each time you change the current BPF stage (not selected, but actually go to the next stage) it forces a CRM save. This is probably refreshing your Option Sets. Add an Xrm.Page.data.process.addOnStageChange event handler, and then run your filter in that.

Daryl
  • 18,592
  • 9
  • 78
  • 145