Skip to content

Commit 452ffcf

Browse files
authored
Get Users Favorite Hierarchy (#2181)
* Create GetUsersFavoriteHierarchy.js * Create README.md
1 parent dd6420a commit 452ffcf

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-0
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// Example call with print out. You can replace gs.getUserID() with a User Name instead
2+
var favorites = getFavoritesHierarchyArray(gs.getUserID());
3+
gs.info(JSON.stringify(favorites));
4+
5+
function getFavoritesHierarchyArray(userId) {
6+
var hierarchy = [];
7+
addGroups("NULL", hierarchy, userId);
8+
addItems("NULL", hierarchy, userId);
9+
return hierarchy;
10+
}
11+
12+
function addGroups(parentId, array, name) {
13+
var grBookmarkGroup = new GlideRecord("sys_ui_bookmark_group");
14+
grBookmarkGroup.addEncodedQuery("user=" + name + "^parent_group=" + parentId);
15+
grBookmarkGroup.query();
16+
while (grBookmarkGroup.next()) {
17+
var groupObj = {
18+
"type": "group",
19+
"color": grBookmarkGroup.getValue("color"),
20+
"order": grBookmarkGroup.getValue("order"),
21+
"title": grBookmarkGroup.getValue("title"),
22+
"items": [],
23+
};
24+
array.push(groupObj);
25+
addGroups(grBookmarkGroup.getUniqueValue(), groupObj.items, name);
26+
addItems(grBookmarkGroup.getUniqueValue(), groupObj.items, name);
27+
}
28+
}
29+
30+
function addItems(parentGroup, array, name) {
31+
var grBookmark = new GlideRecord("sys_ui_bookmark");
32+
grBookmark.addEncodedQuery("user=" + name + "^group=" + parentGroup);
33+
grBookmark.query();
34+
while (grBookmark.next()) {
35+
var grBookmarkObj = {
36+
"type": "bookmark",
37+
"color": grBookmark.getValue("color"),
38+
"order": grBookmark.getValue("order"),
39+
"icon": grBookmark.getValue("icon"),
40+
"open_in_form": grBookmark.getValue("open_in_form"),
41+
"pinned": grBookmark.getValue("pinned"),
42+
"separator": grBookmark.getValue("separator"),
43+
"title": grBookmark.getValue("title"),
44+
"ui_type": grBookmark.getValue("ui_type"),
45+
"uncancelable": grBookmark.getValue("uncancelable"),
46+
"url": grBookmark.getValue("url"),
47+
"flyout": grBookmark.getValue("flyout"),
48+
"flyout_sizing": grBookmark.getValue("flyout_sizing"),
49+
"flyout_width": grBookmark.getValue("flyout_width"),
50+
};
51+
array.push(grBookmarkObj);
52+
}
53+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
This script will allow you to get Favorites Hierarchy of a specific user.
2+
This means all nested groups and links.
3+
4+
Here is an example call with print out. You can replace gs.getUserID() with a User Name instead:
5+
6+
var favorites = getFavoritesHierarchyArray(gs.getUserID());
7+
gs.info(JSON.stringify(favorites));
8+
9+
10+
This will return an array of objects.
11+
Each item in the array will have a "type" property which will be "group" for nested groups and "bookmark" for bookmarks/links
12+
13+
Other properties are named the same as the standard fields on the "sys_ui_bookmark_group" and "sys_ui_bookmark" tables for Groups and Bookmark types respectively

0 commit comments

Comments
 (0)