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 

 

Look like your code is half

 

https://www.servicenow.com/community/itsm-forum/hide-show-select-box-variable-based-on-the-another-s...

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

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

Medi C
Giga Sage

@NowTechie 

You won't need to script, please use UI Policies instead

First one: 

Condition: Choice != A (Choice is not A)

Reverse if false = True

 

UI Policy Action:

- Field: AB.  Visible = Fasle, FieldAction = ClearValue

- Field: AC.  Visible = Fasle, FieldAction = ClearValue

 

Second one:

Condition: Choice != B (Choice is not B)

Reverse if false = True

 

UI Policy Action:

- Field: AB.  Visible = Fasle, FieldAction = ClearValue

 


Thanks & Best regards,
Medi

Dr Atul G- LNG
Tera Patron

Hi @NowTechie 

 

Do it via UI policy that is low code and easy.

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

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

Medi C
Giga Sage

If there is no escape from Client Script, you should take care of the negation as well:

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

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

 


Thanks & Best regards,
Medi

@NowTechie 

 

I hope you are doing well!

Did it work ? Was my reply helpful?


Thanks & Best regards,
Medi