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.