Hide/Show select box variable based on the another select box variable

ronro2
Tera Contributor

Hey guys! 

I want to create an onchange client script for a catalog item based on selection in select box 'order_type', which has three choices: 'new_environment', 'change' and 'decommissioning'.

When 'new_environment' is selected in 'order_type' then we should hide the selection 'no_change' in select box 'node_size' and 'no_change' in select box 'node_amount'.

If you change 'order_type' to 'change' or 'decommissioning' then 'no_change' should be restored and visible.

So when: 

ronro2_0-1742816960343.png

I want to hide: 

ronro2_1-1742817041080.pngronro2_2-1742817076823.png

 

I just can't get it to work. 

Thanks in advance!

1 ACCEPTED SOLUTION

Hello @ronro2 

 

It doesn't works like that with choice type variables. You need to clear the options first. 

Use this ➡️ g_form. clearOptions('<FIELD_NAME>'); 

 

Then if you want to add another option you need one by one add it. 

 

GlideForm - addOption(String fieldName, String choiceValue, String choiceLabel)

 

If only it is independent field not depending on any other field then it would work rhe way you are doing but otherwise if there is dependency. You need to follow above. 

 

Kindly mark my answer as helpful and accept solution if it helped you in anyway. This will help me be recognized for the efforts and also move this questions from unsolved to solved bucket. 

 

Regards,

 

Shivalika 

 

My LinkedIn - https://www.linkedin.com/in/shivalika-gupta-540346194

 

My youtube - https://youtube.com/playlist?list=PLsHuNzTdkE5Cn4PyS7HdV0Vg8JsfdgQlA&si=0WynLcOwNeEISQCY

 

Cc - @Dr Atul G- LNG 

View solution in original post

8 REPLIES 8

Dr Atul G- LNG
Tera Patron
Tera Patron

Hi @ronro2 

 

Try like this:

 

https://www.servicenow.com/community/developer-forum/simple-client-script-to-hide-choice-value-based...

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }

    //Type appropriate comment here, and begin script below
    if (newValue != '1' && newValue != '2') {
      g_form.addOption('u_esito', 'istr','Istruzione');
    } else {
	g_form.removeOption('u_esito', 'istr');
	}
	
}
*************************************************************************************************************
If my response proves useful, please indicate its helpfulness by selecting " Accept as Solution" and " Helpful." This action benefits both the community and me.

Regards
Dr. Atul G. - Learn N Grow Together
ServiceNow Techno - Functional Trainer
LinkedIn: https://www.linkedin.com/in/dratulgrover
YouTube: https://www.youtube.com/@LearnNGrowTogetherwithAtulG
Topmate: https://topmate.io/atul_grover_lng [ Connect for 1-1 Session]

****************************************************************************************************************

Hey @Dr Atul G- LNG ! It is not working. Do you see why? 

function onChange(control, oldValue, newValue, isLoading) {
    // Return if the form is loading or no value is selected
    if (isLoading || newValue === '') {
        return;
    }

    // Check if the selected order type is 'change'
    if (newValue === 'change') {
        // Add 'no_change' as a default option to node_size
        g_form.addOption('node_size', 'no_change', 'Inga förändringar');
        
        // Add 'no_change' as a default option to node_amount
        g_form.addOption('node_amount', 'no_change', 'Inga förändringar');
        
        // Optionally set the default value to 'no_change'
        g_form.setValue('node_size', 'no_change');
        g_form.setValue('node_amount', 'no_change');
		
    } else {
        // Remove 'no_change' options when other order types are selected
        g_form.removeOption('node_size', 'no_change');
        g_form.removeOption('node_amount', 'no_change');
        
        // Clear the values
        g_form.setValue('node_size', '');
        g_form.setValue('node_amount', '');
    }
}

 

@Shivalika  can you help here.

*************************************************************************************************************
If my response proves useful, please indicate its helpfulness by selecting " Accept as Solution" and " Helpful." This action benefits both the community and me.

Regards
Dr. Atul G. - Learn N Grow Together
ServiceNow Techno - Functional Trainer
LinkedIn: https://www.linkedin.com/in/dratulgrover
YouTube: https://www.youtube.com/@LearnNGrowTogetherwithAtulG
Topmate: https://topmate.io/atul_grover_lng [ Connect for 1-1 Session]

****************************************************************************************************************

Ankur Bawiskar
Tera Patron
Tera Patron

@ronro2 

please share your script, it should be an easy one

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader