Hide Variable based on Select Box choices

David Boom
Tera Contributor

I have a select box variable with multiple choices, I need to show other variables based on select box choices.

  • If choice A is selected, variable AB and AC should be visible.
  • If choice B is selected, only variable AB should be visible.

Am able to achieve the condition through onChange client script but when I select A first and then B, it is not hiding AC.

 

if(choice == "A"){

g_form.setVisible('AB', true);

g_form.setVisible('BC', true);

}

 

if(choice == "B"){

g_form.setVisible('AB', true);

}

14 REPLIES 14

Dr Atul G- LNG
Tera Patron
Tera Patron

Hi @David Boom 

I think it would be best is you could use UI policy like 

choice = A

Policy action 

https://www.servicenow.com/community/now-platform-forum/best-way-to-show-hide-fields-on-a-catalog-it...

 

 

NO need to use client script.

*************************************************************************************************************
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]

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

I have around 25 choices and 100 variables. Using UI policies will create around 1000 UI policy actions.

I agree, but since you're facing issues with using CS, I thought that using a UI Policy might be a better approach—it's no-code and quick to implement.

*************************************************************************************************************
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

@David Boom 

try this

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

    // Hide all variables initially
    g_form.setVisible('AB', false);
    g_form.setVisible('AC', false);

    // Show variables based on the selected choice
    if (newValue == 'A') {
        g_form.setVisible('AB', true);
        g_form.setVisible('AC', true);
    } else if (newValue == 'B') {
        g_form.setVisible('AB', true);
    }
}

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