- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-29-2017 12:05 PM
I know this is a bit of an odd question but is there a way to perform a query by the state label instead of the value? Using state.label does not work. (I'm well aware that using the value is by far the safest.)
Solved! Go to Solution.
- Labels:
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-31-2017 02:48 PM
You can try what Shishir has suggested. Or try this
- var inc = new GlideRecord('incident');
- inc.addQuery('state',getState('incident','New'));
- inc.query();
- while (inc.next()) {
- gs.print('-----------'+inc.number);
- }
- function getState(tablename,label)
- {
- var ch = new GlideRecord('sys_choice');
- ch.addQuery('name',tablename);
- ch.addQuery('label',label);
- ch.query();
- if (ch.next())
- {
- return ch.value;
- }
- }
Please mark this response as correct or helpful if it assisted you with your question.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-31-2017 06:23 AM
The default operator is =, I want results where state='New'. Contains should look for instances where the state has the word somewhere in the string, blahNewblah. I tested CONTAINS just as you have listed here to make sure I wasn't missing something and still no result. Did you test this and get a different result?

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-31-2017 07:47 AM
Can we try something like this,
var gr = new GlideRecord('incident');
gr.addJoinQuery('sys_choice', 'sys_class_name', 'name');
gr.addQuery('active', true);
gr.addQuery('label', 'New');
gr.query();
while(gr.next()){
gs.print(gr.number);
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-31-2017 02:48 PM
You can try what Shishir has suggested. Or try this
- var inc = new GlideRecord('incident');
- inc.addQuery('state',getState('incident','New'));
- inc.query();
- while (inc.next()) {
- gs.print('-----------'+inc.number);
- }
- function getState(tablename,label)
- {
- var ch = new GlideRecord('sys_choice');
- ch.addQuery('name',tablename);
- ch.addQuery('label',label);
- ch.query();
- if (ch.next())
- {
- return ch.value;
- }
- }
Please mark this response as correct or helpful if it assisted you with your question.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-02-2018 11:04 AM
This is what I ended up doing...
var inc = new GlideRecord('incident');
var values = getChoiceValues('incident','state',['New','Active'];
inc.addQuery('state','IN',values);
inc.query();
while (inc.next()) {
gs.print('-----------'+inc.number);
}
getChoiceValues: function(table, element, labels){
if (Array.isArray(labels)){
labels = labels.join();
}
var value = [];
var choice = new GlideRecord('sys_choice');
choice.addQuery('name',table);
choice.addQuery('element',element);
choice.addQuery('label','IN',labels);
choice.addQuery('inactive',false);
choice.query();
while (choice.next()) {
value.push(choice.value.toString());
}
if(value.length){
return value.join();
}
return labels;
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-18-2018 09:09 AM
This code is working ,Thanks