Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Hide subcategory option based on category using Catalog Client Script

jaubert
Tera Guru

I am trying to hide a subcategory option from all users that are not itil if a specific category is selected.  Ie.  category "IT Security" is selected then subcategory "Security Exception" is hidden from the list of subcategories.  I am able to do this for the category field but when I add a subcategory nothing happens.  I was able to verify the values for both the category and subcategory using an alert but when I use "g_form.removeOption('subcategory', 'Security Exception');"  It doesn't remove the option.  

 

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


var cat = g_form.getValue('category');
var subcat = g_form.getValue('subcategory');


if(cat == "IT Security" && (!g_user.hasRole('itil'))) {

//alert(cat);

g_form.removeOption('subcategory', 'Security Exception');

}


}

1 ACCEPTED SOLUTION

Mallidi Suma
Tera Guru

Hi @jaubert ,

 

I Executed almost the same code in my PDI Instance.  My client script is an onChange of Category field. It is working perfectly fine.

 

Points to cross-check:-

1. Impersonate with a non-itil User and check.

2. Cross-check the back end of the Category, Subcategory.

3. Check the onChange client script Field value.

 

Please find the attached screenshot: and code:-

MallidiSuma_0-1684165227993.png

Code:-

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var cat = g_form.getValue('category');
var subcat = g_form.getValue('sub_category');
if (cat == "IT Security" && (!g_user.hasRole('itil'))) {
alert("entered");
g_form.removeOption('sub_category', 'Security Exception');
//g_form.removeOption('sub_category','Security Exception','Security Exception');
}
}

 

View solution in original post

3 REPLIES 3

Karan Chhabra6
Mega Sage

Hi @jaubert ,

 

Please double check the backend value of that option, is it 'Security Exception' ,'security_exception' or something else, use the backend value there.

 

If my answer has helped with your question, please mark it as correct and helpful

 

Thanks!

jaubert_0-1684160964018.png

This is the label and value

Mallidi Suma
Tera Guru

Hi @jaubert ,

 

I Executed almost the same code in my PDI Instance.  My client script is an onChange of Category field. It is working perfectly fine.

 

Points to cross-check:-

1. Impersonate with a non-itil User and check.

2. Cross-check the back end of the Category, Subcategory.

3. Check the onChange client script Field value.

 

Please find the attached screenshot: and code:-

MallidiSuma_0-1684165227993.png

Code:-

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var cat = g_form.getValue('category');
var subcat = g_form.getValue('sub_category');
if (cat == "IT Security" && (!g_user.hasRole('itil'))) {
alert("entered");
g_form.removeOption('sub_category', 'Security Exception');
//g_form.removeOption('sub_category','Security Exception','Security Exception');
}
}