Make array unique

Sunny14
Tera Contributor

Hello Team,

 

I have below code which brings records which has duplicate Users.  [licenseRec.user] 

How can I make this into store only unique User records?

Currently both of my array length is coming out same. [i.e. icenseRec  & arrayUnq ] 

 

 

var licenseRec = new GlideRecord('sys_user_has_role');
licenseRec.addEncodedQuery("XXX");
licenseRec.query();
gs.info(licenseRec.getRowCount());

var arrayUnq = [];
while (licenseRec.next()){
    arrayUnq.push(licenseRec.user);
}
gs.info(arrayUnq.length);
 
1 ACCEPTED SOLUTION

@Sunny14 

try this

var licenseRec = new GlideRecord('sys_user_has_role');
licenseRec.addEncodedQuery("xxx");
licenseRec.query();
gs.info(licenseRec.getRowCount());

var arrayUnq = [];
while (licenseRec.next()) {
    arrayUnq.push(licenseRec.user.toString());
}
arrayUnq = new global.ArrayUtil().unique(arrayUnq);
gs.info(arrayUnq.length);
If my response helped please mark it correct and close the thread so that it benefits future readers.
Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

8 REPLIES 8

Bhuvan
Tera Sage

@Sunny14 

 

Try this after while loop,

 

arrayUnq = new global.ArrayUtil().unique(arrayUnq);

 

Thanks,

Bhuvan

Hello Bhuvan, 

 

This is not working as expected. Please let me know if I am making any mistake? 

Thanks.

 

var licenseRec = new GlideRecord('sys_user_has_role');
licenseRec.addEncodedQuery("xxx");
licenseRec.query();
gs.info(licenseRec.getRowCount());

var arrayUnq = [];
while (licenseRec.next()){
    arrayUnq.push(licenseRec.user);
}
arrayUnq = new global.ArrayUtil().unique(arrayUnq);
gs.info(arrayUnq.length);


*** Script: 9811
*** Script: 1

@Sunny14 

try this

var licenseRec = new GlideRecord('sys_user_has_role');
licenseRec.addEncodedQuery("xxx");
licenseRec.query();
gs.info(licenseRec.getRowCount());

var arrayUnq = [];
while (licenseRec.next()) {
    arrayUnq.push(licenseRec.user.toString());
}
arrayUnq = new global.ArrayUtil().unique(arrayUnq);
gs.info(arrayUnq.length);
If my response helped please mark it correct and close the thread so that it benefits future readers.
Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Thank you @Ankur Bawiskar  This worked as expected. 

I think the magic was to add .toString() 

 

Thanks a lot.