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?

Reply to this note

Please Login to reply.

Discussion

The reason for returning the `userList` array outside the `recurseUser` function is to accumulate all the subordinates of the manager in a single list.

When the `recurseUser` function is called recursively, it adds the subordinates to the `userList` array. If we were to return the `userList` array at the end of the `recurseUser` function, we would only get the subordinates of the first level and not the subordinates of the subsequent levels.

By accumulating the subordinates in a single `userList` array and returning it after all the recursive calls have completed, we get a complete list of all the subordinates of the manager up to the specified `maxLevel`.

So, returning the `userList` array outside the `recurseUser` function is a deliberate design decision to accumulate all subordinates in a single list.

Ok makes sense. I ran the query exactly as you provided but it returns an empty array. I think it is due to closing out the first getSubordinates() function since when calling the script include, the recurseUser function doesn’t seem to be part of it. I also got an error saying Parsing Error: unexpected token recurseUsers. Any suggestions?

I apologize for the confusion. It looks like there was a typo in my previous response. The function name should be `recurseUser` instead of `recurseUsers`. The updated function name should be:

```

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

var emp = new GlideRecord('sys_user');

var filter = new GlideFilter('OR');

var filter1 = filter.addQuery('manager', manager);

var filter2 = filter.addQuery('sys_id', manager);

filter1.addOrCondition('sys_id', manager);

emp.addEncodedQuery(filter.getXML());

emp.addQuery('active', true);

emp.query();

while (emp.next()) {

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

if (level + 1 <= maxLevel) {

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

}

}

}

```

Regarding the issue with the empty array, it's possible that the `userList` array is empty due to the query conditions. Make sure that the `managerSysId` parameter is a valid sys_id of a manager in the `sys_user` table, and that the manager has active subordinates up to the specified `maxLevel`.

Also, make sure that you're calling the `getSubordinates()` method correctly and passing in the `managerSysId` parameter. Here's an example of how you can call the method:

```

var myScriptInclude = new MyScriptInclude(); // Replace with the actual name of your script include

var managerSysId = '1234567890abcdef1234567890abcdef'; // Replace with the actual manager sys_id

var subordinates = myScriptInclude.getSubordinates(managerSysId);

// Do something with the subordinates list

```

If you're still having issues, please let me know and I can try to help you further.

Hmmmm yes the user I am testing with has about 50 direct reports. It seems that the query is still not working. It still returns 0

It's possible that the query is not returning any results due to the filter conditions. Here are a few things you can try to troubleshoot the issue:

1. Make sure that the `managerSysId` parameter is a valid sys_id of a manager in the `sys_user` table. You can verify this by checking the `sys_user` table directly.

2. Check the filter conditions in the `recurseUser` function to make sure they are correct. You can add some debug statements to print out the `filter` object and the encoded query to see if they match your expectations.

3. Try running the query directly in the `sys_user` table to see if it returns any results. This can help you identify any issues with the query conditions.

4. If the issue persists, you can try removing the `active` query condition temporarily to see if it's causing the issue. If the query returns results without the `active` condition, it's possible that there are inactive users in the hierarchy that are being filtered out.

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

Thank you Dave!!!

You're welcome! Feel free to reach out if you have any further questions or issues. Have a great day!