Need help in Script

Mark Wood
Tera Contributor

Hello Experts,

I have written the script below, which is almost functioning correctly. However, when I print the results, duplicate values are displayed. I want to obtain unique values. 

 

var manager = [];
var users = [];
var usr = new GlideRecord('sys_user');
usr.addQuery('active',true);
usr.addQuery('sys_id','02826bf03710200044e0bfc8bcbe5d7f');
usr.query();
while(usr.next()){
users.push(usr.name.toString());
var mgr = usr.manager;
}
gs.print("First User: "+users);
manager.push(mgr);
var c=0;
var u1 = new GlideRecord('sys_user');
u1.addQuery('manager',mgr);
u1.addQuery('active',true);
u1.query();
gs.print(u1.getRowCount());
while(u1.next()){
gs.print("User Name: "+u1.name);
if(!users.includes(u1.name.toString())){
 
c++;
users.push(u1.name.toString());
 
}
}
gs.log(users);
//gs.log(c);

 

Could anyone please guide me on what mistakes I might be making? Thank you.

5 REPLIES 5

_Gaurav
Kilo Sage

Hi @Mark Wood 

Can you check the below script to see if this is helpful for you?

var manager = [];
var users = [];
var users1 = [];

var usr = new GlideRecord('sys_user');
usr.addQuery('active', true);
usr.addQuery('sys_id', '02826bf03710200044e0bfc8bcbe5d7f');
usr.query();
if (usr.next()) {
    var userName = usr.sys_id;
    if (!users.includes(userName)) {
        users.push(userName);
    }
    var mgr = usr.manager;
    manager.push(mgr);
}

gs.print("User: " + users);
gs.print("Manager: " + manager);
 
var c = 0;
var u1 = new GlideRecord('sys_user');
for (var i = 0; i < manager.length; i++) {
    u1.addQuery('manager', manager[i]);
}
u1.addQuery('active', true);
u1.query();
while (u1.next()) {
    var userName = u1.name.toString();
    gs.log(userName);
    if (!users1.includes(userName)) {
        users1.push(userName);
    }
}
gs.log(users1);



AshishKM
Kilo Patron
Kilo Patron

Hi @Mark Wood , 

Can you explain your business case, what you are trying to achieve via this code.

 

The first glide result using below will condition will return only 1 record, so you are getting user and his manager in respective arrayList.

usr.addQuery('sys_id', '02826bf03710200044e0bfc8bcbe5d7f');

 

The second glide object u1, any reason you used to for loop , are you trying to add all managers in addQuery() , however there is only ONE manager record in araryList.

 

the below code will not work because, the manager array hold the reference of user's record which are manager to other users but actually they are in same table.

for (var i = 0; i < manager.length; i++) {
    u1.addQuery('manager', manager[i]);
}

Please explain the requirement in details.

 

-Thanks,

AshishKM


Please mark this response as correct and helpful if it helps you can mark more that one reply as accepted solution

Hugo Barros
Tera Contributor

Hey there mark, how you doing? Could you please explain exactly what are you trying to achieve? 
You are doing good so far, but it would be helpful if you tell us the requirement's details, please. Once i get you response i'll dive into it, Thanks!

Hello @Hugo Barros ,

I've been delving into scripting within ServiceNow. Fortunately, my previous issue has been resolved, so there's no urgent need for real-time assistance. I'm sharing the corrected script below for the benefit of future readers who may encounter a similar situation.

 

 

Thank you!

var manager = [];
var users = [];
var usr = new GlideRecord('sys_user');
usr.addQuery('active',true);
usr.addQuery('sys_id','02826bf03710200044e0bfc8bcbe5d7f');
usr.query();
while(usr.next()){
users.push(usr.name.toString());
var mgr = usr.manager;
}
gs.print("First User: "+users);
manager.push(mgr);
var c=0;
var u1 = new GlideRecord('sys_user');
u1.addQuery('manager',mgr);
u1.addQuery('active',true);
u1.query();
gs.print(u1.getRowCount());
while(u1.next()){
gs.print("User Name: "+u1.name);
var arrayUtil=new ArrayUtil();
 
//if(!arrayUtil.contains(users,u1.name.toString())) //this is also working its ootb class in servicenow you can check duplicate in both ways.
if(users.indexOf(u1.name.toString()))
{
 
 
users.push(u1.name.toString());
}
}
gs.log(users);
//gs.log(c);