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 questions based on specific selections

reza1921
Tera Contributor

Hi all,

 

I am creating a catalog item that has a select box with some choices (questions). For example lucky_hs and hifi_gs.

There is another select box right after the one above with specific choices (questions). I am trying to show certain questions in the second select box based off the selection in the first one? Is that possible? I cant seem to add a UI policy action to the variable questions, just the variable itself.

 

Thanks

5 REPLIES 5

Addy22
Tera Guru

You can write a catalog client script and use the g_form.removeOption(). This will solve your query.

 

UI policies can help only in mandatory,visibility and read-only of the variable not to their options

MohammedKhalid
Tera Contributor

 g_form.removeOption()

Vrushali  Kolte
Mega Sage

Hello @reza1921 ,

 

You can create On Change Catalog Client Script on one variable for the catalog item and add the given script as shown in below snapshot -

 

VrushaliKolte_0-1721194302251.png

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

   //Type appropriate comment here, and begin script below

   if(newValue == '1'){
   alert(newValue);
      g_form.clearOptions('field_b_depends_on_field_a');
      g_form.addOption('field_b_depends_on_field_a','A','A');
      g_form.addOption('field_b_depends_on_field_a','B','B');

   } else if(newValue == '2'){
      g_form.clearOptions('field_b_depends_on_field_a');
      g_form.addOption('field_b_depends_on_field_a','A','A');
      g_form.addOption('field_b_depends_on_field_a','C','C');
   }
   //Please replace the variable names and choices 
}

 

VrushaliKolte_1-1721194395019.png

I have tried it in my PDI and it is working for me.

 

If my answer solves your issue, please mark it as Accepted✔️ and Helpful t👍 based on the impact.

Amit Verma
Kilo Patron
Kilo Patron

Hi @reza1921 

 

Below post could be helpful :

https://www.servicenow.com/community/developer-articles/dynamically-create-options-in-a-select-box-b...

 

Instead of having static choices for the second select box, you can make use of g_form.addOption() and g_form.removeOption() method to add or remove the choices based on selection of the first select box in an On-Change Catalog Client Script.

 

Thanks and Regards

Amit Verma


Please mark this response as correct and helpful if it assisted you with your question.