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

How to set up query to search for values in an array

gunishi
Tera Guru

Hi all, 

 

I have an array of asset tags, and I want to check in the cmdb_ci_server table to see if they are there. 

 

I want to make it so that the function returns false if even one of the values in the array is not in the cmbd_ci_server. 

 

I know I could do this as:

 

gr.addQuery('Asset Tag', assetTags[0]); 

gr.addQuery('Asset Tag', assetTags[1]);

 

and so on, but I was wondering if there is a way for me to query the whole array?

 

Thank you for all your help!

 

G

1 ACCEPTED SOLUTION

Vishal Birajdar
Giga Sage

Hi @gunishi 

 

You can convert array into String.

 

Then add encoded query 

 

E.g., var array =[A100,B100]

var arrayStr = array.toString()

gr.addEncodedQuery("asset_tagIN" + arrayStr);

if (gr.next()){

//Record present

} else {

//Not present

}

 

Vishal Birajdar
ServiceNow Developer

I know one thing, and that is that I know nothing.
- Socrates

View solution in original post

10 REPLIES 10

Hi @gunishi 

 

Glad it helps you...!!

 

One more thing you can use your logic in while loop , i given it just as example there.

 

Vishal Birajdar
ServiceNow Developer

I know one thing, and that is that I know nothing.
- Socrates

what if the field is a choice type and need to query with array values? 

I am developing a script include to be used in a widget.

kavithaJ
Tera Contributor

what if the field is a choice field and dynamic array value is to be queried?

I am developing a script include for a dashboard widget.

Hi @kavithaJ ,

 

If possible can you share the exact requirement please...!!

 

 

Vishal Birajdar
ServiceNow Developer

I know one thing, and that is that I know nothing.
- Socrates

 

yes sure