How to hide an option in an select box depending on the catalog item

Ear19
Giga Expert

Is there a script that someone can share with me that will hide options in a select box depending on what catalog item it is.  To provide more information we use a variable set for all access requests.  One of the variables in the variable set is titled "Add or Remove Access" with the options add, remove or modify.  We would only like the modify option to show if it is a certain catalog item.  Is this possible?

1 ACCEPTED SOLUTION

Hey,

There are some issues with your code, please see my example below.

I think it should be on Load

function onLoad() {

    // Array of Catalogue Item SYS_ID's to hide modify option for
    var hideModifyCatItems = ['CAT_ITEM1_SYSID', 'CAT_ITEM2_SYSID', 'CAT_ITEM1_SYSID']

    // Get SYS_ID of current catalog item
    var thisCatItem = g_form.getUniqueValue(); 

    // Is current item sys_id in array of items to hide modify for?
    var hideModifyOnThisItem = hideModifyCatItems .indexOf(thisCatItem) > -1; 

    // what is the name of the variable?
    var variableWithModify = 'your_variable_name';

    if (hideModifyOnThisItem) {
        //Parm 1 - variable name, Parm 2 - value
        g_form.removeOption(variableWithModify,'modify');
    } else {
        g_form.addOption(variableWithModify,'modify');
    }
}

 

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


ServiceNow Nerd
ServiceNow Developer MVP 2020-2022
ServiceNow Community MVP 2019-2022

View solution in original post

16 REPLIES 16

bernyalvarado
Mega Sage

bernyalvarado
Mega Sage

I hope this helps! 🙂

The SN Nerd
Giga Sage
Giga Sage

Create a Client Script on your Catalogue Item that has that Variable Set using the code that @bernyalvarado has mentioned.

API for removeOption() can be found here.

g_form.removeOption('priority', '1');

ServiceNow Nerd
ServiceNow Developer MVP 2020-2022
ServiceNow Community MVP 2019-2022

I have tried code below but can not get it to work.  Should I be using sys ID's in place of the actual name of the catalog item?

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue != 'cat item 1' || 'cat item 2' || 'cat item 3') {

g_form.removeOption('modify');

}

else

g_form.addOption('modify');

return;
}

 

Again the goal is that out of the 30 catalog items that use the same variable set only three of those catalog items should see the modify option on the variable "add_remove_modify".  Thoughts?

JagjeetSingh
Kilo Sage
Kilo Sage

if you are selecting the catalog in one of your variables then create onchange client script on that variable.

if(newValue=="catalog_value")
{
g_form.removeOption('option to be cleared');

}

else{

g_form.addOption('add that option again here');

}

 

if you want to apply this to a catalog item then write a script for that catalog. This will make the code easy.

Let me know if further help required.

 

Jagjeet Singh
ServiceNow Community Rising Star 2022/2023