logo

NJP

Utility script to get all choice labels and values from field of a table

Import · Dec 09, 2021 · article

Some times we require to fetch/print the choice values/labels of a field to perform some validations or print them in an email notifications etc.

So here is a sample script which can be utilized and re-used as per the business requirement.

Note: This works in both global and scoped application

I just took an example of incident and change_request table and field as close_code

Script: For incident table

var choices = GlideChoiceList.getChoiceList("incident", "close_code");

for (var i = 0; i < choices.getSize(); i++){
        var choiceValue = choices.getChoice(i).getValue();
        var choiceLabel = choices.getChoice(i).getLabel();
        gs.info("Choice Label -> " + choiceLabel + " Choice Value ->" + choiceValue);

}

Choice Table:

image

Output:

image

Another Example for change_request table and field as close_code

Script: For change_request table

var choices = GlideChoiceList.getChoiceList("change_request", "close_code");

for (var i = 0; i < choices.getSize(); i++){
        var choiceValue = choices.getChoice(i).getValue();
        var choiceLabel = choices.getChoice(i).getLabel();
        gs.info("Choice Label -> " + choiceLabel + " Choice Value ->" + choiceValue);

}

Choice Table:

image

Output:

image

Thanks for reading the blog and do provide your inputs/suggestions if any.

Hope you find this article helpful. Don’t forget to Mark it Helpful, Bookmark.Thanks,

Ankur Bawiskar

ServiceNow MVP 2021,2020,2019,2018

Developer MVP 2021

My Articles & Blogs

View original source

https://www.servicenow.com/community/developer-blog/utility-script-to-get-all-choice-labels-and-values-from-field-of/ba-p/2275362