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

I am working on a widget creation on a portal in which the dashboard information are based on the agent who logs in.

where my part is to create a script include for widget functionality (for onLoad, onChange).

The widget (dashboard) provides the count of certain reports and that is based on the values we select on the 6 drop down list boxes(on the widget) where 5 of these drop down list values are based on the choices appearing in assignment group drop down field (values based on current logged in user).

 

While working on the onChange function,

1) I need to get the input values from client side (when agent selects the values in those 6 drop down fields) and pass it by calling my onChange function.

2) The report results (rowCount) are generated based on the combination of all the 6 drop down field values (AND operation).

3) So in some cases, one of the values may be passed as array. (When agent chooses ALL for some drop down fields (example: Category, Priority, Status)). These are choice fields in ServiceNow instance, so I couldnt pass the values directly to the addQuery logic.

 

Is there a way to achieve it please?

 

Code:

onChange: function(groupList, arrCategory, arrPriority, arrProduct, arrChannel, arrStatus) { //userId: SysID of user account
        groupList: Array of assignment group in which user is member of */
        var gIncident = new GlideRecord("incident");
        gIncident.addQuery("assignment_group.name", "IN", groupList.toString()); (working fine as it is a text field)
        gIncident.addQuery("category", "IN", arrCategory.toString());      (not working)
        gIncident.addQuery("priority", "IN", arrPriority.toString());  (not working)
        gIncident.addQuery("u_product_description.display_name", "IN", arrProduct.toString());  (not working)
        gIncident.addQuery("contact_type", "IN", arrChannel.toString());  (not working)
        //gIncident.addQuery("state", "IN", arrStatus.toString());  (not working)
        gIncident.addEncodedQuery("sys_updated_onONToday@javascript:gs.beginningOfToday()@javascript:gs.endOfToday()^state=6");
        gIncident.query();
        var rCount = gIncident.getRowCount();
 
}