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.

Select Box choices dependant on another Select Box

kemmy1
Tera Guru

Can I do this without creating a new table?

I have two variable select boxes.  

The first one has choices A, B and C.

My second select box has choices, but they are dependant on what is selected on Select Box one.

Someone suggested that I add my choices to variable Select Box one (like normal), and then with Select Box two, choose the sys_choice table for "Choice Table" and Value for "Choice Field."

Then go to they sys_choice table and add:

Table: sc_req_item

Element: (my select box one field)

Lable: the label of one of my select box two choices

Value: the value of one of my select box two choices

depends on: the choice of on of the select box one choice.

and keep adding for each select box 2 choice.

Unfortunately this is not working.   Am I missing something?   Or is the only way to do this is with a table and a client script?

19 REPLIES 19

These are choices that are already in a table.   But this helps in the future!   And this if for a Service Catalog Item as well.


You can use onChange catalog client script. Script looks like this (assumes variables are named category and subcategory):



function onChange(control, oldValue, newValue, isLoading) {


      if (isLoading) {


              return;


      }


      if(newValue == oldValue){


              return;


      }


     


      //remove all items from subcat drop down to start


      // Used the g_form.clearOptions() function instead of g_form.removeOption() function


      g_form.clearOptions('subcategory');


      g_form.addOption('subcategory', '', '-- None --', '0');


     


      var ga = new GlideAjax("CatalogAjax");


      ga.addParam("sysparm_name", "getSubcategories");


      ga.addParam("sysparm_cat", newValue);


     


      ga.getXML(ajaxResponse);


     


      function ajaxResponse(serverResponse) {


             


              var result = serverResponse.responseXML.getElementsByTagName("result");



              var subcat = serverResponse.responseXML.getElementsByTagName("subcat");


              for(var i = 0; i < subcat.length; i++) {


                      var name = subcat[i].getAttribute("name");


                      var value = subcat[i].getAttribute("value");


                      var sequence = subcat[i].getAttribute("sequence");


                      g_form.addOption('subcategory', value, name, sequence);


              }


      }


}


mounika107
Tera Contributor

this video is not suitable for catalog items

Nikhil Dixit
Giga Expert

Hi Kemmy,

 

I had resolve this issue for my one of the requirements. I had created two 'Select Box' type variable where second variable (Model Types) was depend on the selection on first variable (Laptop manufacturer).

I used catalog client script in order to achieve it.

 

find_real_file.png

 

find_real_file.png

I believe it will be serve your purpose. Please mark answer correct and helpful based on the response.

 

Regards,

Nikhil Dixit

find_real_file.png

www.DxSherpa.com

 

This is an older post but I tried this, added in the onChange script and my field is blank, it's not putting any of the values in the drop down.  

 

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

var getSd = g_form.getValue('variables.edge_model');

if(getSd == "5x0"){
g_form.clearOptions('variables.site_tier');
g_form.addOptions('variables.site_tier', 'tier_1', 'Tier 1');
g_form.addOptions('variables.site_tier', 'm_a', 'M&A');

}else if(getSd == "840"){
g_form.clearOptions('variables.site_tier');
g_form.addOptions('variables.site_tier', 'tier_1', 'Tier 1');
g_form.addOptions('variables.site_tier', 'tier_2', 'Tier 2');
g_form.addOptions('variables.site_tier', 'tier_3', 'Tier 3');
g_form.addOptions('variables.site_tier', 'data_center', 'Data Center');
g_form.addOptions('variables.site_tier', 'm_a', 'M&A');

}else if(getSd == "1000"){
g_form.clearOptions('variables.site_tier');
g_form.addOptions('variables.site_tier', 'tier_1', 'Tier 1');
g_form.addOptions('variables.site_tier', 'tier_2', 'Tier 2');
g_form.addOptions('variables.site_tier', 'tier_3', 'Tier 3');
g_form.addOptions('variables.site_tier', 'data_center', 'Data Center');
g_form.addOptions('variables.site_tier', 'm_a', 'M&A');

}else if(getSd == "2000"){
g_form.clearOptions('variables.site_tier');
g_form.addOptions('variables.site_tier', 'tier_1', 'Tier 1');
g_form.addOptions('variables.site_tier', 'tier_2', 'Tier 2');
g_form.addOptions('variables.site_tier', 'tier_3', 'Tier 3');
g_form.addOptions('variables.site_tier', 'data_center', 'Data Center');
g_form.addOptions('variables.site_tier', 'm_a', 'M&A');

}else if(getSd == "Virtual"){
g_form.clearOptions('variables.site_tier');
g_form.addOptions('variables.site_tier', 'tier_1', 'Tier 1');
g_form.addOptions('variables.site_tier', 'tier_2', 'Tier 2');
g_form.addOptions('variables.site_tier', 'tier_3', 'Tier 3');
g_form.addOptions('variables.site_tier', 'data_center', 'Data Center');
g_form.addOptions('variables.site_tier', 'm_a', 'M&A');

}else {
g_form.clearOptions('variables.site_tier');
}

}