Skip to content

Commit f57aa40

Browse files
authored
Get my reports dynamic filters (#809)
* Create readme.md * Create getMyDirectReports.js * Create getMyDirectReportsBRandDynamicFilters.xml * Update readme.md * Update readme.md * Update getMyDirectReports.js This looping script traverses the User table from a certain point to get either one level of employees or all employees in the hierarchy underneath the logged-on user. There are two functions: 1. **getMyDirectReports**: gets only users directly reporting to the logged on user 1. **getMyReports**: gets all users reporting to the logged on user * Delete Business Rules/getMyDirectReports/getMyDirectReportsBRandDynamicFilters.xml Removed XML for BR. I recommend that users create this new in their instance based on the details from the readme. * Move to new Dynamic Filter folder * moved to Dynamic Filter folder * fixed relative link in readme.md
1 parent 691da31 commit f57aa40

File tree

5 files changed

+106
-231
lines changed

5 files changed

+106
-231
lines changed

Business Rules/getMyDirectReports/getMyDirectReports.js

Lines changed: 0 additions & 50 deletions
This file was deleted.

Business Rules/getMyDirectReports/getMyDirectReportsBRandDynamicFilters.xml

Lines changed: 0 additions & 146 deletions
This file was deleted.

Business Rules/getMyDirectReports/readme.md

Lines changed: 0 additions & 35 deletions
This file was deleted.
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
function getMyDirectReports(managerID) {
2+
//if no User sys_id is passed in, assume we need the direct reports for the logged on user.
3+
if (typeof managerID == "undefined") {
4+
managerID = gs.getUserID();
5+
}
6+
7+
//unique method is used to ensure no duplicates
8+
var myPeople = []; //array for direct reports sys_ids
9+
10+
var userGr = new GlideRecord("sys_user");
11+
userGr.addQuery("manager", managerID);
12+
userGr.addQuery("sys_id", "!=", managerID); //recursion protection, exclude the manager
13+
userGr.addQuery("active", true); //only active employees
14+
userGr.query();
15+
if (!userGr.next()) {
16+
//return early if no direct reports
17+
return myPeople;
18+
}
19+
while (userGr.next()) {
20+
myPeople.push(userGr.getUniqueValue());
21+
}
22+
23+
// remove duplicates
24+
var arrayUtil = new ArrayUtil();
25+
arrayUtil.unique(myPeople);
26+
27+
return myPeople;
28+
}
29+
30+
function getMyReports(managerID) {
31+
//if no User sys_id is passed in, assume we need the direct reports for the logged on user.
32+
if (typeof managerID == "undefined") {
33+
managerID = gs.getUserID();
34+
}
35+
36+
var arrayUtil = new ArrayUtil(); //unique method is used to ensure no duplicates
37+
var myReports = []; //array for direct reports sys_ids
38+
39+
getReportsRecursive(managerID, myReports);
40+
41+
arrayUtil.unique(myReports);
42+
return myReports;
43+
}
44+
45+
function getReportsRecursive(managerID, myReports) {
46+
var arrayUtil = new ArrayUtil();
47+
48+
var userGr = new GlideRecord("sys_user");
49+
userGr.addQuery("manager", managerID);
50+
userGr.addQuery("sys_id", "!=", managerID); //recursion protection 1, exclude the manager
51+
userGr.addQuery("active", true); //only active employees
52+
userGr.query();
53+
while (userGr.next()) {
54+
var userID = userGr.getUniqueValue();
55+
//recursion protection 2, only proceed with a new search if this user isn't already in the array.
56+
if (!arrayUtil.contains(myReports, userID)) {
57+
myReports.push(userID);
58+
//keep following the org hierarchy until we don't find any more reports
59+
getReportsRecursive(userID, myReports);
60+
}
61+
}
62+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
This looping script traverses the User table from a certain point to get either one level of employees or all employees in the hierarchy underneath the logged-on user. There are two functions:
2+
1. **getMyDirectReports**: gets only users directly reporting to the logged on user
3+
1. **getMyReports**: gets all users reporting to the logged on user
4+
5+
This solution has three components: one Global Business rule and two Dynamic Filters.
6+
* Admins can use the script as a Reference Qualifier
7+
* End Users can select the predefined filter in lists and reports (like with "One of My Assignments").
8+
9+
There is some recursion protection; the script checks to see if it has already collected the User before it tries to get their direct reports.
10+
11+
**IMPORTANT: The use of this script could have performance impacts for very large organizations. Use at your discretion.**
12+
13+
**Business Rule**
14+
15+
| Field | Value |
16+
|---|---|
17+
| Name | getMyDirectReports |
18+
| Table | Global [global] |
19+
| Advanced | true |
20+
| Script | <em>see [getMyDirectReports.js](getMyDirectReports.js) in this folder</em> |
21+
22+
**Dynamic Filter Option (sys_filter_option_dynamic)**
23+
24+
| Field | Value |
25+
|---|---|
26+
| Label | One of My Direct Reports |
27+
| Script | getMyDirectReports() |
28+
| Field type | Reference |
29+
| Reference Table | User [sys_user] |
30+
| Order | 500 |
31+
| Reference script | Business Rule: getMyDirectReports |
32+
| Available for filter | true |
33+
| Available for ref qual | true |
34+
35+
| Field | Value |
36+
|---|---|
37+
| Label | One of My Reports |
38+
| Script | getMyReports() |
39+
| Field type | Reference |
40+
| Reference Table | User [sys_user] |
41+
| Order | 600 |
42+
| Reference script | Business Rule: getMyDirectReports |
43+
| Available for filter | true |
44+
| Available for ref qual | true |

0 commit comments

Comments
 (0)