Skip to content

Commit 46a37c9

Browse files
authored
Get my reports dynamic filters - fixed! (ServiceNowDevProgram#830)
1 parent b6adf70 commit 46a37c9

File tree

6 files changed

+89
-78
lines changed

6 files changed

+89
-78
lines changed
27.1 KB
Loading

Dynamic Filters/getMyDirectReports/getMyDirectReports.js

Lines changed: 0 additions & 62 deletions
This file was deleted.
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
var getMyReportsUtil = Class.create();
2+
getMyReportsUtil.prototype = {
3+
4+
initialize: function() {
5+
this.myReports = []; //array for direct reports sys_ids
6+
this.managerID = gs.getUserID(); //set Manager ID to the logged on User
7+
},
8+
9+
getMyDirectReports: function() {
10+
//query the user table for users that report to the specifed Manager
11+
var userGr = new GlideRecord("sys_user");
12+
userGr.addQuery("manager", this.managerID);
13+
userGr.addQuery("sys_id", "!=", this.managerID); //recursion protection, exclude the manager
14+
userGr.addQuery("active", true); //only active employees
15+
userGr.query();
16+
while (userGr.next()) {
17+
//add the User to the Array
18+
this.myReports.push(userGr.getUniqueValue());
19+
}
20+
21+
return this.myReports;
22+
},
23+
24+
getMyReports: function() {
25+
this._getReportsRecursive();
26+
27+
return this.myReports;
28+
},
29+
30+
_getReportsRecursive: function() {
31+
//query the user table for users that report to the specifed Manager
32+
var userGr = new GlideRecord("sys_user");
33+
userGr.addQuery("manager", this.managerID);
34+
userGr.addQuery("sys_id", "!=", this.managerID); //recursion protection 1, exclude the manager
35+
userGr.addQuery("sys_id", "NOT IN", this.myReports); //recursion protection 2, exclude previously found users
36+
userGr.addQuery("active", true); //only active employees
37+
userGr.query();
38+
while (userGr.next()) {
39+
//add the User to the Array
40+
this.myReports.push(userGr.getUniqueValue());
41+
//keep following the org hierarchy until we don't find any more reports
42+
this.managerID = userGr.getUniqueValue();
43+
this._getReportsRecursive();
44+
}
45+
},
46+
47+
48+
49+
type: 'getMyReportsUtil'
50+
};
55.5 KB
Loading
74.1 KB
Loading
Lines changed: 39 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,67 @@
1+
# Summary #
12
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
3+
1. **One of My Direct Reports**: gets only users directly reporting to the logged on user
4+
1. **One of My Reports**: gets all users reporting to the logged on user
45

5-
This solution has three components: one Global Business rule and two Dynamic Filters.
6+
> [!WARNING]
7+
> There is some recursion protection, but the use of this script could have performance impacts for very large organizations. Use at your discretion.
8+
9+
**How to Use**
610
* 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").
11+
* Users (with platform access) can select the predefined filter in lists and reports (like with "One of My Assignments").
812

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.
13+
# Solution #
14+
This solution has three main components:
15+
* 1 Client Callable Script Include
16+
* 2 Dynamic Filters
1017

11-
**IMPORTANT: The use of this script could have performance impacts for very large organizations. Use at your discretion.**
18+
> [!NOTE]
19+
> You will also need to create an ACL for your client callable script include. Think through other security rules before using this filter broadly for users without roles. OOTB Users without roles are often restricted to seeing only their items, for example, a User without itil or snc_incident_read typically cannot see other user's Incidents unless they are the Opened by or are on the Watch list. Make sure you test as the users you hope to publish this item to.
1220
13-
**Business Rule**
21+
## Script Include (sys_script_include) ##
1422

1523
| Field | Value |
1624
|---|---|
17-
| Name | getMyDirectReports |
18-
| Table | Global [global] |
19-
| Advanced | true |
20-
| Script | <em>see [getMyDirectReports.js](getMyDirectReports.js) in this folder</em> |
25+
| Name | getMyReportsUtil |
26+
| Client Callable | true |
27+
| Script | <em>see [getMyReportsUtil.js](getMyReportsUtil.js)</em> |
28+
29+
## Dynamic Filter Option (sys_filter_option_dynamic) ##
2130

22-
**Dynamic Filter Option (sys_filter_option_dynamic)**
31+
### One of My Direct Reports ###
2332

2433
| Field | Value |
2534
|---|---|
2635
| Label | One of My Direct Reports |
27-
| Script | getMyDirectReports() |
36+
| Script | new global.getMyReportsUtil().getMyDirectReports() |
2837
| Field type | Reference |
2938
| Reference Table | User [sys_user] |
3039
| Order | 500 |
31-
| Reference script | Business Rule: getMyDirectReports |
40+
| Roles | Choose the same role(s) you added to the Script Include ACL |
41+
| Reference script | Script Include: getMyReportsUtil |
3242
| Available for filter | true |
3343
| Available for ref qual | true |
3444

45+
**Example Incident list filtered by Caller is One of My Direct Reports**
46+
![Example Incident list filtered by Caller is One of My Direct Reports](one_of_my_direct_reports.png)
47+
48+
### One of My Reports ###
49+
3550
| Field | Value |
3651
|---|---|
3752
| Label | One of My Reports |
38-
| Script | getMyReports() |
53+
| Script | new global.getMyReportsUtil().getMyReports() |
3954
| Field type | Reference |
4055
| Reference Table | User [sys_user] |
4156
| Order | 600 |
42-
| Reference script | Business Rule: getMyDirectReports |
57+
| Roles | Choose the same role(s) you added to the Script Include ACL |
58+
| Reference script | Script Include: getMyReportsUtil |
4359
| Available for filter | true |
4460
| Available for ref qual | true |
61+
62+
**Example Incident list filtered by Caller is One of My Reports**
63+
![Example Incident list filtered by Caller is One of My Reports](one_of_my_reports.png)
64+
65+
### Example Manager Data for the screenshots above ###
66+
67+
![Example Manager Data](example_manager_data.png)

0 commit comments

Comments
 (0)