The Zurich release has arrived! Interested in new features and functionalities? Click here for more

ArrayUtil unique not working

Jalen
Tera Contributor

I have this code that I am using in a fix script and I am trying to get an unique array. The ArrayUtils does not work. Can someone tell me whats wrong with this?

/ Get all users
var userGr = new GlideRecord('sys_user');
userGr.addQuery('sys_id', 'user sys_id);
userGr.query();
 
while (userGr.next()) {
    // Get all auths assigned to the user
    var userAuthGr = new GlideRecord('auth_table');
    userAuthGr.addQuery('u_user', userGr.sys_id);
    userAuthGr.query();
 
    while (userAuthGr.next()) {
var auths = [];
auths.push(userAuthGr.u_user_authorization.sys_id);
 
    }
    // Remove duplicates from the auths array
    var au = new ArrayUtil();
var list = auths.toString().split(',');
    var finAuths = au.unique(list);
gs.log(":whats in this array auth " + finAuths);

 

 

2 REPLIES 2

BryGuy
Tera Contributor

When you push to the array, be sure to pass in the value with a .toString(). I.e. 

auths.push(userAuthGr.u_user_authorization.sys_id.toString());
 
You have a syntax error here - an unnecessary " ' " next to the user.sys_id.
userGr.addQuery('sys_id', 'user sys_id);
 
 

Jalen
Tera Contributor

Only one sys_id is being displayed. Any idea why?

 

Here is the remainder of the script: 

// Remove existing auths assigned to the user
var existingAuthsGr = new GlideRecord('auth table');
 existingAuthsGr.addQuery('u_user', userGr.sys_id);
 existingAuthsGr.deleteMultiple();

// Assign the unique roles to the user
for (var i = 0; i < finAuths.length; i++) {
var newAuth = new GlideRecord('auth table');
 newAuth.initialize();
 newAuth.u_user = userGr.sys_id;
 newAuth.u_user_authorization = finAuths[i];
 newAuth.insert();
   }

}