Pass array in GlideRecord query

Servicenow Use4
Kilo Guru

Hello!

I am trying to pass an array of values to addQuery, but I am not getting the desired output.

Form a previous code snippet, I get an array of CI sys IDs (arrayCI, suppose), which is also correct. I have verified that. I have to pass this array to below gliderecord.

Below is that sample of code:

var totalCount=0;

var taskCIObject = new GlideRecord('task_ci');

taskCIObject.addEncodedQuery(<<some encoded query>>); //which is working fine

taskCIObject.addQuery(<<pass the array that I got previously>>); //not sure of the syntax

taskCIObject.query();

while(taskCIObject.next())

{ totalCount = totalCount +1;} //the count is getting wrongly computed

gs.addInfoMessage('Count = '+totalCount);

 

I want to get the count of records for which the set of CIs is exact same.

Can anyone please help me solve the above?

Thanks!

 

 

1 ACCEPTED SOLUTION

Hi,

is array contains CI sys_ids then do this

taskCIObject.addQuery('ci_item', 'IN', arr.toString());

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

View solution in original post

5 REPLIES 5

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

for querying with multiple values present in array use this syntax

var arr = ['sys_id1','sys_id2','sys_id3'];

taskCIObject.addQuery('fieldName', 'IN', arr.toString()); //not sure of the syntax

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Hi Ankur!

I tried taskCIObject.addQuery('fieldName', 'IN', arr.toString())

but the output is still incorrect.

Hi,

are you using correct field name and comparing with correct array values.

Example this should work fine for incident and caller

var arr = ['userSysId1','userSysId2'];

var rec = new GlideRecord('incident');

rec.addQuery('caller_id', 'IN', arr.toString());

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Hi.. yes the column names are correct.. my requirement is "I want to get the count of records for which the set of CIs is exact same."

The set is of CIs is an array. Table 'task_ci' stores the Affected CIs for Change and other task types... I need to know how many change requests exists for which exact same CIs are present under Affected CI tab in change form.