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.

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

Thanks mark it helped

anand-bhosle
Tera Guru

That worked after making a small tweak on the query. Thanks @Mark Roethof 🙂