Skip to content

Commit 6e1637b

Browse files
authored
Get All Child Roles (#2280)
* Create GetAllChildRolesRecursive.js * Create README.md
1 parent 79011a2 commit 6e1637b

File tree

2 files changed

+125
-0
lines changed

2 files changed

+125
-0
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// You can set this to a role name or the Sys ID of the "sys_user_role" record
2+
var roleNameOrSysId = "itil";
3+
var result = getFullRoleHierarchyList(roleNameOrSysId);
4+
5+
// This is just to print out the results. Remove this when you want to use it
6+
for (var i = 0; i < result.length; i++) {
7+
gs.info(result[i]);
8+
}
9+
10+
function getFullRoleHierarchyList(roleNameOrSysID) {
11+
12+
var roleSysID = "";
13+
var grRole = new GlideRecord("sys_user_role");
14+
grRole.addEncodedQuery("name=" + roleNameOrSysID + "^ORsys_id=" + roleNameOrSysID);
15+
grRole.query();
16+
while(grRole.next()) {
17+
roleSysID = grRole.getUniqueValue();
18+
}
19+
20+
if (!roleSysID) {
21+
gs.warn("Role entered does not exist.")
22+
return;
23+
}
24+
25+
var obj = getChildRoles({}, roleSysID, "", "role", "contains");
26+
var arr = [];
27+
for (var key in obj) {
28+
arr.push(obj[key].display);
29+
}
30+
return arr;
31+
}
32+
33+
function getChildRoles(found, queryId, display, queryField, getField) {
34+
if (!queryId || found.hasOwnProperty(queryId))
35+
return;
36+
37+
if (display) {
38+
found[queryId] = {display: display};
39+
direct = false;
40+
}
41+
var gr = new GlideRecord("sys_user_role_contains");
42+
gr.addEncodedQuery(queryField + "=" + queryId);
43+
gr.query();
44+
while(gr.next()) {
45+
getChildRoles(found, gr.getValue(getField), gr.getDisplayValue(getField), queryField, getField);
46+
}
47+
48+
return found;
49+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
Use this script to get a flattened array of all Roles contained by a specificed role.
2+
This will recursively drill down into other contained roles so you have the full list of roles involved in the hierarchy.
3+
4+
To use, you can set the "roleNameOrSysId" variable to either a role name or the Sys ID from the "sys_user_role" table
5+
Or call the "getFullRoleHierarchyList" function directly and pass in a role name or Sys ID
6+
7+
The following lines exist just to print out the results for your testing. Remove this if needed:
8+
9+
for (var i = 0; i < result.length; i++) {
10+
gs.info(result[i]);
11+
}
12+
13+
14+
15+
- Example input: "it_project_manager"
16+
17+
- List of chiold roles found in the Related List for "it_project_manager":
18+
19+
resource_user
20+
idea_manager
21+
it_demand_manager
22+
pa_viewer
23+
it_project_user
24+
project_manager
25+
timeline_user
26+
27+
- Example output and full list of nested child roles for "it_project_manager":
28+
29+
resource_user
30+
report_group
31+
report_user
32+
viz_creator
33+
skill_user
34+
idea_manager
35+
it_demand_manager
36+
it_project_user
37+
it_project_portfolio_user
38+
project_portfolio_user
39+
sn_gf.goal_user
40+
sn_gf.goal_user_read
41+
sn_gf.strategy_planner_read
42+
it_demand_user
43+
demand_user
44+
baseline_user
45+
pps_resource
46+
project_user
47+
timecard_user
48+
sn_test_management.tester
49+
planning_console_user
50+
task_editor
51+
sn_gf.strategy_planner
52+
timeline_user
53+
demand_manager
54+
scrum_user
55+
cmdb_read
56+
scrum_admin
57+
rm_scrum_task_admin
58+
rm_doc_admin
59+
rm_task_admin
60+
rm_test_admin
61+
rm_story_admin
62+
rm_epic_admin
63+
rm_release_scrum_admin
64+
rm_sprint_admin
65+
view_changer
66+
financial_mgmt_user
67+
fiscal_calendar_user
68+
sn_invst_pln.std_user
69+
rate_model_user
70+
sn_invst_pln_v2.investment_user
71+
sn_invst_pln_investment_user
72+
currency_instance_report_admin
73+
pa_viewer
74+
project_manager
75+
timecard_approver
76+
sn_test_management.test_manager

0 commit comments

Comments
 (0)