- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-31-2020 07:21 AM
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?
Solved! Go to Solution.
- Labels:
-
Scripting and Coding
-
Team Development

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-01-2020 06:16 PM
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');
}
}
ServiceNow Nerd
ServiceNow Developer MVP 2020-2022
ServiceNow Community MVP 2019-2022
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-01-2020 06:29 AM
So I think what I need is to hide the modify option for the majority of the catalog items and only visible for three catalog items. Would the code remain the same. I want to be able to use something that will work with multiple catalog items. So maybe instead of by catalog item the client script can be for the variable set that would hide the 'modify' option if it is not a certain catalog item?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-01-2020 06:50 AM
You can write the code on variable set as well. In this case you first have to query sc_cat_item table. Here the complex part is querying the table. You can use glideRecord function but that is not recommended as per best practices. Instead use script include and GlideAjax functionalities. See below links for more information see below links:
https://snprotips.com/blog/2016/2/6/gliderecord-client-side-vs-server-side
You can go with the client script on individual catalog items if performance isn't the issue. As you say there are only two or three such catalog items. You have to use the script in those only.
Let me know if you want to assistance on this. Share the form layout and variables with me.
ServiceNow Community Rising Star 2022/2023
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-01-2020 07:09 AM
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;
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-01-2020 07:58 AM
You do not have to change anything in first section.
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
//write your code after this.
I can help better once i have a look on the form. Otherwise its like aiming in dark. I am not getting few things. Like are you selecting catalog item from reference field in form?
If not then OnChange script will work.
ServiceNow Community Rising Star 2022/2023
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-01-2020 12:00 PM
Thank you so much for responding. No the catalog item is being selected from the Service Portal. What other things do you need to know? See screenshot of client script as well as the client script:
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;
}