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

Dr Atul G- LNG
Tera Patron
Tera Patron

If I am not wrong, you can get right click in field and configure choice and see all drop down values. 

*************************************************************************************************************
If my response proves useful, please indicate its helpfulness by selecting " Accept as Solution" and " Helpful." This action benefits both the community and me.

Regards
Dr. Atul G. - Learn N Grow Together
ServiceNow Techno - Functional Trainer
LinkedIn: https://www.linkedin.com/in/dratulgrover
YouTube: https://www.youtube.com/@LearnNGrowTogetherwithAtulG
Topmate: https://topmate.io/atul_grover_lng [ Connect for 1-1 Session]

****************************************************************************************************************

AtulyaLNG_0-1700063601916.png

@JordanUnito 

*************************************************************************************************************
If my response proves useful, please indicate its helpfulness by selecting " Accept as Solution" and " Helpful." This action benefits both the community and me.

Regards
Dr. Atul G. - Learn N Grow Together
ServiceNow Techno - Functional Trainer
LinkedIn: https://www.linkedin.com/in/dratulgrover
YouTube: https://www.youtube.com/@LearnNGrowTogetherwithAtulG
Topmate: https://topmate.io/atul_grover_lng [ Connect for 1-1 Session]

****************************************************************************************************************

Hi Atulya, 

 

This isn't a particularly helpful answer, no - I'm not talking here about choosing options in the ui, where it's clear that you can see the options; As I mentioned in the post, the goal is to be doing this through code for any table dynamically, since the integration I'm building will potentially be connecting to any and all of a customer's tables, and we want to display all of the choices in our UI for those particular kinds of fields. 

 

Martin Ivanov gave a solid answer above - that's the one I'll be using. Thank you for your input nonetheless, though!

 

praveenhirekudi
Tera Contributor

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