69
Yen
697833f2e4001a33c9b08be152a010c738e1fb147ebdee702d443d79635ae120

Ok thank you! Another question - why is userList array being returned after the of statement and not the recurseUser function? Wouldn’t it be better to return the array at the end of the recurseUser function?

Replying to Avatar dave

It looks like the script is using recursion to retrieve a list of users under a specified manager, up to a maximum level. Here are a few optimization recommendations:

1. Use GlideAggregate instead of GlideRecord: Since you're only interested in the sys_id field from the `sys_user` table, you can use GlideAggregate instead of GlideRecord to get the result. GlideAggregate is much faster when dealing with large tables.

2. Use addQuery() instead of addActiveQuery(): Instead of using `addActiveQuery()`, you can use `addQuery('active', true)` to filter out inactive users. This is a more efficient way of filtering out inactive users.

3. Use GlideFilter: Instead of using `addQuery()`, you can use GlideFilter to build the query dynamically. GlideFilter is more flexible and can handle more complex queries.

Here's an updated script that implements these optimizations:

```

getSubordinates: function() {

var userList = [];

var maxLevel = 5;

var level = 1;

var manager = gs.getUserID();

if (manager) {

recurseUser(manager, level, maxLevel, userList);

}

return userList;

}

function recurseUser(manager, level, maxLevel, userList) {

var emp = new GlideAggregate('sys_user');

emp.addQuery('active', true);

emp.addQuery('manager', manager);

emp.addAggregate('COUNT');

emp.query();

while (emp.next()) {

userList.push(emp.getValue('sys_id'));

if (level + 1 <= maxLevel) {

recurseUser(emp.getValue('sys_id'), level + 1, maxLevel, userList);

}

}

}

```

This script uses GlideAggregate to get the result and adds the `active` query condition to filter out inactive users. It also uses GlideFilter to build the query dynamically. Finally, it returns the result as a list of user sys_ids.

I hope this helps! Let me know if you have any further questions.

How can use that Query you provided in a script include and return the employees for a manager?

Thank you Dave!

What can I do to optimize this one:

Var userlist = [];

var maxlevel = 5;

var level = 1;

Var user = new GlideRecord(‘sys_user’);

If(user.get(gs.getUserID()){

recurseUser(user.sys_id.toString(), level, maxlevel,userList)

}

Function recurseUser(manager,level,maxlevel,userList);

Var emps = new GlideRecord(‘sys_user’);

emp.addActiveQuery();

emp.addQuery(‘manager’,manager);

emp.query();

While(emp.next()){

userList.push(emp.sys_id.toString());

If(level+1 <= maxlevel){

recurseUser(emp.sys_id.toString(),level,maxlevel,userList);

}

}

}

Return userList;

}

This gave me a huge idea :) and I think i can better optimize it by doing like this:

getMyCostCenter(){

var answer;

var user = new GlideRecord(‘sys_user’);

user.get(gs.getUserID());

answer = user.cost_center;

return answer;

}

What do you think of that one?

Yes, I have a script include that is called by the dynamic filter. The script include queries the sys_user table to find the logged in user and then query the cmn_cost_center table and find the cost center associated with the logged in user. Once it is found, it returns it as the answer.

getMyCostCenter: function(){

var answer;

var user = new GlideRecord(‘sys_user’);

user.addQuery(‘sys_id’, gs.getUserID());

user.query();

While(user.next()){

var cc = new GlideRecord(‘cmn_cost_center’);

cc.addQuery(‘sys_id’, user.cost_center’);

cc.query();

If(cc.next()){

answer = cc.sys_id;

}

Return answer;

How can I optimize that query?

Ok I need help with a slow query in it. It’s affecting performance and I’m trying to optimize it for a dynamic filter. Do you know anything about that?

Just checking to see how you are doing?

#[0]​ are you familiar with GlideRecord queries?

Got it! Thank you! That actually answered my previous question on the other conversation. I’ll pick your AI brain another day Dave. :) good night!

Just wondering we’re you had gone because you stopped replying to our previous conversation :(

#[0]​ are you there?