g_form removeOption does not work on catalog variable

tkaneko
Giga Contributor

I am trying to remove an option from a select box on a catalog variable but it does not remove. The script is:

function onChange(control, oldValue, newValue, isLoading) {

    if (isLoading) {

          return;

    }

  g_form.removeOption('eai_reviewer_sel_next_task', 'eai_research');

  //g_form.clearOptions('eai_reviewer_sel_next_task');

  //g_form.addOption('eai_reviewer_sel_next_task', 'uat_implement', 'UAT Implementation');

  //g_form.addOption('eai_reviewer_sel_next_task', 'prod_implement', 'PROD Implementation');

}

The g_form.clearOptions and g_form.addOption functions seem to work fine but the g_form.removeOption does not.   I would prefer to not clear all options and add back the ones I need as I need to include the 'none' option and its setMandatory properties.

1 ACCEPTED SOLUTION

nickbuhl
Tera Contributor

In case you are still having this problem or someone else stumbles upon this, I was having a similar problem with getControl(), clearOptions() and removeOption() not running on Catalog Tasks or Requested Items when I found this article.


ServiceNow KB: The g_form.getControl method does not work after ordering (KB0547171)


I added the suggested code below to my client scripts and used form.removeOption instead of g_form.removeOption.


var form = typeof g_sc_form != "undefined" ? g_sc_form : g_form;   


View solution in original post

30 REPLIES 30

Chuck Tomasi
Tera Patron

Hi Tyler,



This is the first I have heard of this being a problem.



Are you certain eai_research is an exact match for the value (not the label) of the option you want to remove.


Yes, I verified that 'eai_research' is the value and not the label.


Here is a screenshot of the variable:


find_real_file.png


Something is off. I just tried the same thing with a select box on a catalog item. I called mine u_number and added options


One,one


Two,two


Three,three



then created an onLoad client script that removed option 1



g_form.removeOption('u_number', 'one');



and it worked. Not sure what to tell you. Did you double check that the client script is active and doing an onChange on the right variable?


I think the only difference between your script and mine is I am running on onChange on another catalog variable.   I did read that if the current option is selected you cannot remove the option so I added a clearValue function before I try to remove the option.   I am going to try it with an onLoad script to see if it will work that way.



The client script is working because I have uncommented the clearOptions and addOption functions and it performs as expected.



function onChange(control, oldValue, newValue, isLoading) {


    if (isLoading) {


          return;


    }



g_form.clearValue('eai_reviewer_sel_next_task');


g_form.removeOption('eai_reviewer_sel_next_task', 'eai_research');