How to get display value of choice field values?

Sandeep Reddy L
Tera Contributor

Hi guys,

I have a table and it has a choice field ('u_model_name'). I want to query the choice field and i would like to get its labels.

following is my code which i have run in Background scripts:

var choice = [];
var audi = new GlideRecord('u_audi_sales_1');
audi.query();
while (audi.next()) {
var abc = audi.getElement("u_model_name").getChoices();
}
choice.push(abc);
gs.print(choice);

when i run this code i am getting choice Values but i want label. for Eg :  label: "Audi A4" and value: "1", label: "Audi Q8" and value: "2" like this i have 14 choices when i run this code code i am getting 

*** Script: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]. but i want to fetch labels.

can any one suggest me.

  

1 ACCEPTED SOLUTION

Do you executed the Script in Global Context? Are the choices of the field u_model_name defined direct in the table u_audi_sales_1 or you have hierarchy of tables and some parent defined the choices? For example, if you execute the script

var sysChoice = new GlideRecord("sys_choice");
sysChoice.addQuery("name", "incident");
sysChoice.addQuery("element", "state");
sysChoice.addQuery("language", "en");
sysChoice.orderBy("sequence");
sysChoice.query();
var table, element, label, infos = [];
while (sysChoice.next()) {
    gs.print("sequence=" + sysChoice.sequence + "; value=" + sysChoice.value + "; label=" + sysChoice.label);
}

then you get the following results

sequence=1; value=1; label=New
sequence=2; value=2; label=Active
sequence=3; value=3; label=Awaiting Problem
sequence=4; value=4; label=Awaiting User Info
sequence=5; value=5; label=Awaiting Evidence
sequence=6; value=6; label=Resolved
sequence=7; value=7; label=Closed

but if you would change "state" to "impact", the results will be empty. The parent of "incident" table is "task" table and because the "impact" not defined on "incident" table then the choices from the parent table "task" will be used:

var sysChoice = new GlideRecord("sys_choice");
var tableHierarchy = GlideDBObjectManager.getTables("incident").toArray();
gs.print(tableHierarchy.join());
sysChoice.addQuery("name", "task"); //"incident");
sysChoice.addQuery("element", "impact") // "state");
sysChoice.addQuery("language", "en");
sysChoice.orderBy("sequence");
sysChoice.query();
var table, element, label, infos = [];
while (sysChoice.next()) {
    gs.print("sequence=" + sysChoice.sequence + "; value=" + sysChoice.value + "; label=" + sysChoice.label);
}

One can easy make a look over tableHierarchy array to get all choices in more common case.

View solution in original post

5 REPLIES 5

Hi,

yes it was Extended from another table. i have changed the name .Now its working

Thanks for your valuable time for explaining me.

regards,

Sandeep Reddy Lebaka