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.

How to hide the values of subcategories

SmithO
Kilo Contributor

Hello Everyone,

 

We have a field called 'category' on the incident form, and based on the selected category, the subcategory field values are displayed. We have options A, B, C, and D for the category field, and options 1, 2, 3, 4, and 5 for the subcategory field. However, we want to hide the subcategory option '4' when 'B' is selected in the category field. How can we achieve this?

 

Note: We only want to hide this option on the form.

5 REPLIES 5

Addy22
Tera Guru

If it is to remove an option, you can go for onChange client script 

 

var category = g_form.getValue('category');
if (category == "B") {
g_form.removeOption('sub_category', '4');
}

Deepak Shaerma
Kilo Sage
Kilo Sage

Hi @SmithO 

Create a new Client Script ,

Name: Adjust Subcategory Options
Table: Incident
Type: onChange
Field: Category

 var categoryValue = g_form.getValue('category');
    
    if (categoryValue == 'B') {
        // If category is ‘B’, hide subcategory option ‘4’
        g_form.removeFieldOption('subcategory', '4');

Please Mark this Helpful and Accepted Solution. If this Helps you to understand. This will help both the community and me..
- Keep Learning ‌‌
Thanks & Regards
Deepak Sharma 


dgarad
Giga Sage

Hi @SmithO 

try the below code.

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

if (isLoading || newValue == '') {
return;
}

if(newValue == 'B') {
g_form.clearValue('sub_category','');
g_form.addOption('sub_category', '1', '1' );
g_form.addOption('sub_category', '2', '2' );
g_form.addOption('sub_category', '3', '3' );
g_form.addOption('sub_category', '5', '5' );

}else{
g_form.addOption('sub_category', '1', '1' );
g_form.addOption('sub_category', '2', '2' );
g_form.addOption('sub_category', '3', '3' );
g_form.addOption('sub_category', '4', '4' );
g_form.addOption('sub_category', '5', '5' );

}

 

If my answer finds you well, helpful, and related to the question asked. Please mark it as correct and helpful.

Thanks
dgarad

Jitendra Diwak1
Kilo Sage

Hi @SmithO ,

 

You can apply the onChange client script to achieve this. code is given below:

 

}var cat = g_form.getValue('category');
    
    if (cat == 'B') {// cat ==backend value of category
        // If category is selectes as ‘B’ then hide subcategory option ‘4’
        g_form.removeOption('subcategory', '4');
}

 

Please accept my solution if it works for you and thumps up

 

Thanks

Jitendra

Please accept my solution if it works for and thumps up.