Skip to content

Commit 56aef89

Browse files
authored
Create GetAllChildRolesRecursive.js
1 parent f409e84 commit 56aef89

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-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+
}

0 commit comments

Comments
 (0)