Display Choice field values depend on other fields

anand-bhosle
Tera Guru

Hello VA gurus,

I have a topic where user inputs a category field. I would like to add an option to users to select subcategory field as well but i want to show only those sub category fields which depend on category field. How do i make that happen? Below is the code and flow that i have created. Any suggestions/help will be much appreciated.


var options=[];
var choices = new GlideRecord('sys_choice');
//choices.addQuery('dependent_value','hr');
choices.addQuery('table', 'table');
choices.addQuery('element','u_subcategory');
choices.query();

while(choices.next()){
options.push({value: choices.value,label: choices.value});
gs.info(options);
}

 

find_real_file.png

 

Working code for Urgency is below:

(function execute() {
var choices = newGlideChoiceList();
// Getting all the choices for urgency.
var choiceList = choices.getChoiceList('incident', 'urgency');
 
var options = [];

for (var i=0; i < choiceList.getSize(); i++) {
options.push({'value': choiceList.getChoice(i).getValue(), 'label': choiceList.getChoice(i).getLabel()});
}
 
return options;
})()

 

Thanks

Anand

2 ACCEPTED SOLUTIONS

Mark Roethof
Tera Patron
Tera Patron

Hi there,

The use of new GlideChoiceList is a nice one! Though actually... I don't know if there's a similar API for dependent values.

I don't have a set up currently for HR to my availability, so I will give an example based on incident.

I guess with the example working code for urgency, you can set up your own category code. Based on incident for example:

(function execute() {

  var choices = new GlideChoiceList();
  var choiceList = choices.getChoiceList('incident', 'category');
 
  var options = [];
  for (var i=0; i < choiceList.getSize(); i++) {
    options.push({'value': choiceList.getChoice(i).getValue(), 'label': choiceList.getChoice(i).getLabel()});
  }
 
  return options;

})()

Looking for the dependent subcategory, again I don't know if there's a similar API, so scripted it in this case:

(function execute() {

  var choices = new GlideRecord('sys_choice');
  choices.addQuery('inactive', false);
  choices.addQuery('dependent_value', vaInputs.category); // Rename this vaInputs to your Category user input
  choices.addQuery('language', 'en');
  choices.orderBy('order');
  choices._query();

  var options = [];
  while(choices._next()) {
    options.push({'value': choices.getValue('value'), 'label': choices.getValue('label')});
  }

  return options;

})()

(= tested, works)

If my answer helped you in any way, please then mark it as helpful.

Kind regards,
Mark
2020 ServiceNow Community MVP
2020 ServiceNow Developer MVP

---

LinkedIn
Community article list

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn

View solution in original post

anand-bhosle
Tera Guru

That worked after making a small tweak on the query. Thanks @Mark Roethof ðŸ™‚ 

View solution in original post

6 REPLIES 6

Marcelle Howard
ServiceNow Employee
ServiceNow Employee

Hi Anand,

Is this design part of a topic, or part of a setup topic? If it is part of a topic, is this a guided process where the user would select an option, which would then prompt a related sub-option? 

HR is setup differently than ITSM in that it uses a hierarchy structure (COE > Topic Category > Topic Detail > HR Service) to route the case appropriately to the correct resolution team. You also need to consider cross-scoping implications with RCA and make sure to use the HR VA scope if the topic will create a case.

The easier way to configure a category/sub-category experience (if using this in a topic) is to have your category, then use the decision utility to branch to the different sub-category options, which will in turn have their own flows. The end result will look more like a tree structure than have the dependency format. 

You may also want to create separate topics depending on the complexity of the topic design.

Hope this helps.

Thanks,

Marcelle

Hello Marcelle, yes it is part of a topic and a guided process to let the user select options. I am using reference choice under user input section. Decision utility something which makes complex flow.

HR is one of the category and i wanted to show subcategory values which are depend on HR category. If i don't use the script i can able to show all sub categories but that doesnt help user to choose right sub-category. Does that make sense?

Mark Roethof
Tera Patron
Tera Patron

Hi there,

The use of new GlideChoiceList is a nice one! Though actually... I don't know if there's a similar API for dependent values.

I don't have a set up currently for HR to my availability, so I will give an example based on incident.

I guess with the example working code for urgency, you can set up your own category code. Based on incident for example:

(function execute() {

  var choices = new GlideChoiceList();
  var choiceList = choices.getChoiceList('incident', 'category');
 
  var options = [];
  for (var i=0; i < choiceList.getSize(); i++) {
    options.push({'value': choiceList.getChoice(i).getValue(), 'label': choiceList.getChoice(i).getLabel()});
  }
 
  return options;

})()

Looking for the dependent subcategory, again I don't know if there's a similar API, so scripted it in this case:

(function execute() {

  var choices = new GlideRecord('sys_choice');
  choices.addQuery('inactive', false);
  choices.addQuery('dependent_value', vaInputs.category); // Rename this vaInputs to your Category user input
  choices.addQuery('language', 'en');
  choices.orderBy('order');
  choices._query();

  var options = [];
  while(choices._next()) {
    options.push({'value': choices.getValue('value'), 'label': choices.getValue('label')});
  }

  return options;

})()

(= tested, works)

If my answer helped you in any way, please then mark it as helpful.

Kind regards,
Mark
2020 ServiceNow Community MVP
2020 ServiceNow Developer MVP

---

LinkedIn
Community article list

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn

Hello Mark, I have tried the same code earlier but it is not filtering any values nor it is showing anything. When i tried the same code in background script i get null object and if i use array variable i will get [Object, Object]. any other way to handle this situation?

 

Thanks

Anand