Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Help getting unique values from list collector

gjz
Mega Sage

I have an array of sys ids based on a query, but the array has duplicates in it and I'm not able to get unique values using ArrayUtil.unique() and I don't know why.

 

The query is working as I expect, multiple divisions can have the same department.

 

var eQuery = 'u_liaisonsLIKE' + '49f32c82dbd89340197c38ff9d9619e1';
var arrayUtil = new ArrayUtil();
var myDept = [];
 
var d = new GlideRecord('u_cmn_divisions');
d.addEncodedQuery(eQuery);
d.query();
while(d.next()) {
myDept.push(d.u_department.sys_id);
}
gs.print('myDept with dups: ' + myDept);
myDept = arrayUtil.unique(myDept);
gs.print('myDept no dups: ' + myDept);
gs.print('myDept: ' + arrayUtil.unique(myDept));
 
I get the same list no matter what - does anyone know why this doesn't work?
 
gjz_1-1714503495777.pnggjz_2-1714503543070.png

 


 

1 ACCEPTED SOLUTION

James Chun
Kilo Patron

Hi @gjz,

 

You can use the GlideAggregate to group the query together, so you won't have to use the use the ArrayUtil.

e.g.

var myDept = [];
var eQuery = 'u_liaisonsLIKE' + '49f32c82dbd89340197c38ff9d9619e1';
var agg = new GlideAggregate('u_cmn_divisions');
agg.addEncodedQuery(eQuery);
agg.groupBy('u_department');
agg.query();
while(agg.next())
{
	myDept.push(agg.getValue('u_department'));
}

 

Back to your script, try replacing myDept.push(d.u_department.sys_id); to 

myDept.push(d.getValue('u_department'));

 

 Cheers

View solution in original post

2 REPLIES 2

James Chun
Kilo Patron

Hi @gjz,

 

You can use the GlideAggregate to group the query together, so you won't have to use the use the ArrayUtil.

e.g.

var myDept = [];
var eQuery = 'u_liaisonsLIKE' + '49f32c82dbd89340197c38ff9d9619e1';
var agg = new GlideAggregate('u_cmn_divisions');
agg.addEncodedQuery(eQuery);
agg.groupBy('u_department');
agg.query();
while(agg.next())
{
	myDept.push(agg.getValue('u_department'));
}

 

Back to your script, try replacing myDept.push(d.u_department.sys_id); to 

myDept.push(d.getValue('u_department'));

 

 Cheers

Works like a charm!  Thanks!