The CreatorCon Call for Content is officially open! Get started here.

gliderecord query by state label

Jim60
Tera Expert

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.)

1 ACCEPTED SOLUTION

You can try what Shishir has suggested. Or try this



  1. var inc = new GlideRecord('incident');
  2. inc.addQuery('state',getState('incident','New'));
  3. inc.query();
  4. while (inc.next()) {
  5.     gs.print('-----------'+inc.number);
  6. }
  7. function getState(tablename,label)
  8. {
  9. var ch = new GlideRecord('sys_choice');
  10. ch.addQuery('name',tablename);
  11. ch.addQuery('label',label);
  12. ch.query();
  13. if (ch.next())
  14. {
  15. return ch.value;
  16. }
  17. }

Please mark this response as correct or helpful if it assisted you with your question.

View solution in original post

11 REPLIES 11

SanjivMeher
Kilo Patron
Kilo Patron

Hi Jim,



Yes, You can query based on display Value. Try something like below using CONTAINS. it worked for me.




var inc = new GlideRecord('incident');




inc.addQuery('category','CONTAINS','Request');


inc.query();




while (inc.next())


{


gs.addInfoMessage('-----------'+inc.number);


}



Please mark this response as correct or helpful if it assisted you with your question.

Thank you for taking time to reply.   I'm looking specifically for the 'state' field and this does not seem to work.   (Where state of New is a valid label)


var inc = new GlideRecord('incident');


inc.addQuery('state','New');


inc.query();


while (inc.next()) {


    gs.print('-----------'+inc.number);


}


You can use getChoiceValue(); to get the label for the choice field, but querying the table always works with the name, value pairs.


Hope to see some method like this.


You didnt use contains. Use below script



  1. var inc = new GlideRecord('incident');  
  2. inc.addQuery('state','CONTAINS','New');  
  3. inc.query();  
  4. while (inc.next()) {  
  5.     gs.print('-----------'+inc.number);  
  6. }  

Please mark this response as correct or helpful if it assisted you with your question.