Is it possible to get a list of all possible values of any given field?

JordanUnito
Tera Contributor

 

Hey folks,
 
I'm working on an app to integrate dynamically with any given serviceNow table that a user picks, and then interact with the data in that table. In most cases this is straightforward, but specifically in the case of fields like State, Priority, Urgency, or various other fields with a pre-determined list of valid values, the API only returns a number, as opposed to the "friendly name" of that number. In the reference docs, there are some examples of a map of the numerical values to "friendly names", but A) they vary from table to table and B) they're customizeable, so if we hard-code that map, it will be quite possible that we'll give the wrong friendly name for a given table, even if it's correct on another. 
 
My question is this: Is there any way to, through the API, query for the possible values (including display names) of any given field? Ideally, there would be some endpoint that would give me a json object of all possible values for a given field, or something to that effect.
 
Thanks in advance!
 
 
 
1 ACCEPTED SOLUTION

Martin Ivanov
Giga Sage
Giga Sage

Hello. Here is a simple script that will retrieve data from the sys_choice table, where all the choice definitions reside.

MartinIvanov_0-1699817840457.png

In this example I am retrieving all active values of the state field on the inciden table.

var grChoice = new GlideRecord('sys_choice');
grChoice.addQuery('name', 'incident'); //replace 'incident' with your table
grChoice.addQuery('element', 'state'); //replace 'state' with your field name
grChoice.addQuery('inactive', false);
grChoice.addQuery('language', 'en') //replace dynamically if you have more than one language.
grChoice.query();

while(grChoice.next()){
    gs.info('Choice value: '+ grChoice.getValue('value') + ' Choice label: '+ grChoice.getValue('label'));
}

 

 


Please mark Correct and click the Thumb up if my answer helps you resolve your issue. Thanks!
Martin Ivanov
ServiceNow MVP 2023, 2024

View solution in original post

8 REPLIES 8

Martin Ivanov
Giga Sage
Giga Sage

Hello. Here is a simple script that will retrieve data from the sys_choice table, where all the choice definitions reside.

MartinIvanov_0-1699817840457.png

In this example I am retrieving all active values of the state field on the inciden table.

var grChoice = new GlideRecord('sys_choice');
grChoice.addQuery('name', 'incident'); //replace 'incident' with your table
grChoice.addQuery('element', 'state'); //replace 'state' with your field name
grChoice.addQuery('inactive', false);
grChoice.addQuery('language', 'en') //replace dynamically if you have more than one language.
grChoice.query();

while(grChoice.next()){
    gs.info('Choice value: '+ grChoice.getValue('value') + ' Choice label: '+ grChoice.getValue('label'));
}

 

 


Please mark Correct and click the Thumb up if my answer helps you resolve your issue. Thanks!
Martin Ivanov
ServiceNow MVP 2023, 2024

@JordanUnito  Did my answer help you resolve your issue? Do not forget to mark it as Correct.


Please mark Correct and click the Thumb up if my answer helps you resolve your issue. Thanks!
Martin Ivanov
ServiceNow MVP 2023, 2024

Thank you Martin - this should work. didn't realize there was a separate table for it. Thank you!

 

From a element descriptor you can get the dictionary elements of the field. I am using following code for the same.
var gr = new GlideRecord('incident');
gs.setLimit(1);
gr.query();
if (gr.next())
{
var choices = gr.getElement('state).getChoices();
// need list of choice display values
}
------------------------------------------------------------

getChoices() method returns Java Array list. j2js() function helped to convert it to array. Saved me a lot of effort. 

 

var glideRecord = new GlideRecord('incident');
glideRecord.query('priority','1');
glideRecord.next();

// urgency has choice list: 1 - High, 2 - Medium, 3 - Low, with value: 1, 2, 3
var choices = glideRecord.urgency.getChoices();
gs.info(choices);
var choiceList = j2js(glideRecord.urgency.getChoices());
gs.print(choiceList.length);
for (var i=0; i < choiceList.length; i++ ) {
gs.print (choiceList[i]);
}

 

hope this helps