Utility script to get all choice labels and values from field of a table
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:
Output:
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:
Output:
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
https://www.servicenow.com/community/developer-blog/utility-script-to-get-all-choice-labels-and-values-from-field-of/ba-p/2275362