We're reclaiming inactive PDIs to keep them available for active builders. Learn what's changing, who's affected, and how to protect your work. Read More

Hide Variable based on Select Box choices

NowTechie
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

Hi @NowTechie 

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.

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

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

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.

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

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

Ankur Bawiskar
Tera Patron

@NowTechie 

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  ||  ✨ 10x ServiceNow MVP  ||  ✨ ServiceNow Community Leader