How do you change what choices are shown based on the choices made in another variable in a record producer?

mahudg2
Kilo Expert

I'm creating a record producer where a category and subcategory is shown.

Subcategory and category are both custom variables that are drop down lists. But I want the subcategories to be based off what category is shown.

For example when someone chooses phone for the category I'd like the subcategory to show phone related items.

 

Would this be a good place to use variable sets?

1 ACCEPTED SOLUTION

mduluk
Giga Expert

I don't see a dependent in catalog variables like they have in Choice lists on forms.   What you might need to do is use the catalog UI policies.     Make a different sub-cause for each choice in the cause.   So if you had 5 causes, then there would be 5 different sub cause variables.     Then use the UI policies to hide the different sub causes until the correct cause is chosen.   This sounds messy, so hopefully someone else can come up with a better idea.


View solution in original post

14 REPLIES 14

Dan Tolgyesi1
Tera Expert

Hi Matt,



If you are going to use the Category/Subcategory on multiple forms, I would definately recommend a variable set.



The link below will take you to a wiki article that should help you create the dependencies



https://wiki.servicenow.com/index.php?title=Adding_Dependent_Variables



Thanks,


Dan


Thank you for the answer but I guess I'm still confused . I'm still not sure how variable sets work and how I can use variable sets with this method.


Hi Matt,



When variables are grouped together they are called variable sets. variable sets are reusable. They can be used across different catalog items. However normal variable belong to that specific item only. Following is the link.


Service Catalog Variable Sets - ServiceNow Wiki



Regarding Dependent choice list, you can use simple client script based.


You will need following things:


On change of category


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


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('subcat');



//build a new list of dependent options


var gp = new GlideRecord('sys_choice');


gp.addQuery('dependent_value', newValue);


gp.addQuery('element', 'subcategory');


gp.query();


while(gp.next()){


  g_form.addOption('subcat', gp.value, gp.label);


}


}


Additional OnLoad script to initially clear the Subcategory  

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


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


g_form.clearOptions('subcat');


}


bajarang..g_form.clearOptions('u_sub_2'); does not work on Internet Explorer .



Is there any thing else that will clearOptions from IE ?