Need help in Script
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-06-2024 08:24 AM - edited 02-06-2024 10:01 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-06-2024 08:41 AM
Hi @Mark Wood
Can you check the below script to see if this is helpful for you?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-06-2024 08:44 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-07-2024 06:25 AM
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!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-07-2024 11:05 AM - edited 02-07-2024 11:06 AM
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);