
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-28-2019 05:54 AM
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.
Solved! Go to Solution.
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-28-2019 08:07 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-28-2019 06:08 AM
You can get all information about choices from sys_choice table:
var sysChoice = new GlideRecord("sys_choice");
sysChoice.addQuery("name", "u_audi_sales_1"); // the name of your htable
sysChoice.addQuery("element", "u_model_name"); // the name of the field
sysChoice.addQuery("language", "en"); // language of labels, which you need
sysChoice.orderBy("sequence");
sysChoice.query();
var table, element, label;
while (sysChoice.next()) {
gs.print("sequence=" + sysChoice.sequence + "; value=" + sysChoice.value + "; label=" + sysChoice.label);
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-28-2019 07:27 AM
Hi OlegKi,
it was not giving any result.
regards,
sandeep Reddy Lebaka.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-28-2019 08:07 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-28-2019 08:36 AM
I improved a little previously posted code. New code
function getChoicesExact (table, field, language) {
var choiceInfos = [], table, element, label, infos = [],
sysChoice = new GlideRecord("sys_choice");
sysChoice.addQuery("name", table);
sysChoice.addQuery("element", field);
sysChoice.addQuery("inactive", "false");
sysChoice.addQuery("language", language);
sysChoice.orderBy("sequence");
sysChoice.query();
while (sysChoice.next()) {
choiceInfos.push({
sequence: String(sysChoice.sequence),
value: String(sysChoice.value),
label: String(sysChoice.label),
dependent_value: String(sysChoice.dependent_value),
hint: String(sysChoice.hint),
});
}
return choiceInfos;
}
function getChoices (table, field, language) {
var i, infos, tableHierarchy = GlideDBObjectManager.getTables("incident").toArray();
for (i = 0; i < tableHierarchy.length; i++) {
infos = getChoicesExact(tableHierarchy[i], field, language);
if (infos.length > 0) {
break;
}
}
return infos;
}
gs.print(JSON.stringify(getChoices("incident", "state", "en")));
gs.print(JSON.stringify(getChoices("incident", "impact", "en")));
displays
[{"sequence":"1","value":"1","label":"New","dependent_value":"","hint":""},{"sequence":"2","value":"2","label":"Active","dependent_value":"","hint":""},{"sequence":"3","value":"3","label":"Awaiting Problem","dependent_value":"","hint":""},{"sequence":"4","value":"4","label":"Awaiting User Info","dependent_value":"","hint":""},{"sequence":"5","value":"5","label":"Awaiting Evidence","dependent_value":"","hint":""},{"sequence":"6","value":"6","label":"Resolved","dependent_value":"","hint":""},{"sequence":"7","value":"7","label":"Closed","dependent_value":"","hint":""}]
[{"sequence":"1","value":"1","label":"1 - High","dependent_value":"","hint":""},{"sequence":"2","value":"2","label":"2 - Medium","dependent_value":"","hint":""},{"sequence":"3","value":"3","label":"3 - Low","dependent_value":"","hint":""}]