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

How to query multiple values in a 'state'

Sandeep109
Mega Contributor

Hi All,

I wanted to check if an incident record is available with either resolved state or closed state or cancelled state

is below script for querying the table right?

inc.addQuery('state=7'||'state=6'||'state=8');   // is this querying right?? as am not getting the output

Please advice

Regards,

Sandeep

 




1 ACCEPTED SOLUTION

Hayo Lubbers
Kilo Sage

Hi,

 

The suggested addEncodedQuery is 1 option.

You can also do it via the addOrCondition:

 
var grIncident2 = new GlideRecord('incident');
grIncident2.addQuery('incident_state',6).addOrCondition('incident_state',7).addOrCondition('incident_state',8);
grIncident2.query();
gs.print("COUNT IS : " + grIncident2.getRowCount());

Or even via the addQuery with IN:

 
var grIncident = new GlideRecord('incident');
grIncident.addQuery('incident_stateIN6,7,8');
grIncident.query();
gs.print("COUNT IS : " + grIncident.getRowCount());​
Regards,
Hayo

View solution in original post

7 REPLIES 7

vkachineni
Kilo Sage

Use servicenow UI  to generate the queries

 

find_real_file.png

stateIN1,2,3

Use

inc.addEncodedQuery('stateIN1,2,3'); 

Please mark Correct and click the Thumb up if my answer helps you resolve your issue. Thanks!
Vinod Kumar Kachineni
Community Rising Star 2022

Saurav11
Kilo Patron
Kilo Patron

Hello,

Please use it as 

inc.addEncodedQuery('stateIN6,7,8'); 

Please mark answer correct/helpful based on Impact

Hello Sandeep,

Just saw you had marked my answer as correct and then changed it.

Any reason why?

Sunny Khandelwa
Kilo Guru

Hi Sandeep,

 

Try the below code in Background script. It will give you the count of either Incident resolved or closed or cancelled state.

var incstate = new GlideRecord('incident');
incstate.addEncodedQuery('incident_stateIN6,7,8');
incstate.query();
gs.print("COUNT IS--" + incstate.getRowCount());

 

If you want some manipulation after this

 

var incstate = new GlideRecord('incident');
incstate.addEncodedQuery('incident_stateIN6,7,8');
incstate.query();
while (incstate.next()

{

//Your code

}