Remove options from a form on Service Portal (g_form.removeOption() trouble)

Student1
Tera Contributor

Hello all, I need some assistance to the following requirement.

I have a record producer on SP, where depending on the category selection, only related subcategories are visible.

For instance, for the category "hardware", only the following subcategories are visible.

find_real_file.png

However, if I change to another category (for example "software"), all the options are not visible (only the option "--None--" is visible). Then, if I return back to the category "hardware", all the previous options (shown on the first image) are not visible any more as shown below.

find_real_file.png

I am using a Catalog Client Script (onChange), that gets the value of the field category, and based on that value removes all the unrelated options on the subcategory field. My script looks like this:

find_real_file.png

Any assistance would be really helpful. 

Thank you all in advance.

 

1 ACCEPTED SOLUTION

Jaspal Singh
Mega Patron
Mega Patron

Hi,

 

Try using format as below

g_form.removeOption('variable_name','value','label');

So, 

g_form.removeOption('subcategory','11','CPU'); //suppose 11 is value while CPU is label.

 

In addition, you need to use g_form.addOption() as well. So, for  your case suppose 

if(cat==8)

{

g_form.removeOption('subcategory','11','CPU');

..

g_form.addOption('subcategory','7'); //choice you want to show

...

}

 

So, for every .removeOption() you need to use .addOption() to add & make it work independently.

View solution in original post

6 REPLIES 6

Jaspal Singh
Mega Patron
Mega Patron

Hi,

 

Try using format as below

g_form.removeOption('variable_name','value','label');

So, 

g_form.removeOption('subcategory','11','CPU'); //suppose 11 is value while CPU is label.

 

In addition, you need to use g_form.addOption() as well. So, for  your case suppose 

if(cat==8)

{

g_form.removeOption('subcategory','11','CPU');

..

g_form.addOption('subcategory','7'); //choice you want to show

...

}

 

So, for every .removeOption() you need to use .addOption() to add & make it work independently.

Thank you!! It seems that it works without any issues.

Regards,

Tomas.

Hi,

While you got the solution, one thing to notice is to remove a option, you need only the fieldname and value. You don't need label.

Ref: https://developer.servicenow.com/dev.do#!/reference/api/orlando/client/c_GlideFormAPI#r_GlideFormRem...

Ex: g_form.removeOption('subcategory','11');

But For add, you need 3 arguments fieldname, value, label

Ex: g_form.addOption('subcategory','7','Label'); //choice you want to show

Ref: https://developer.servicenow.com/dev.do#!/reference/api/orlando/client/c_GlideFormAPI#r_GlideformAdd...

Mark the comment as helpful if it helps.

Thank you very much.

It helps, since I am new to SN.

Thanks again for your time.