Skip to content

Commit 4040096

Browse files
Create code.js
1 parent 3d66b44 commit 4040096

File tree

1 file changed

+41
-0
lines changed
  • Server-Side Components/Script Includes/Recursive GlideRecord Fetcher

1 file changed

+41
-0
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
var RecursiveFetcher = Class.create();
2+
RecursiveFetcher.prototype = {
3+
initialize: function(tableName, parentField) {
4+
this.tableName = tableName;
5+
this.parentField = parentField;
6+
this.visited = [];
7+
},
8+
9+
fetchChildren: function(parentSysId) {
10+
if (this.visited.indexOf(parentSysId) !== -1) {
11+
// Avoid infinite loops
12+
return [];
13+
}
14+
15+
this.visited.push(parentSysId);
16+
var children = [];
17+
18+
var gr = new GlideRecord(this.tableName);
19+
gr.addQuery(this.parentField, parentSysId);
20+
gr.query();
21+
22+
while (gr.next()) {
23+
var child = {
24+
sys_id: gr.getValue('sys_id'),
25+
name: gr.getDisplayValue('name') || gr.getDisplayValue('short_description'),
26+
children: this.fetchChildren(gr.getValue('sys_id')) // Recursive call
27+
};
28+
children.push(child);
29+
}
30+
31+
return children;
32+
},
33+
34+
type: 'RecursiveFetcher'
35+
};
36+
37+
// Example usage:
38+
//var fetcher = new RecursiveFetcher('task', 'parent');
39+
//var hierarchy = fetcher.fetchChildren('abc123sysid'); // Replace with actual parent sys_id
40+
//gs.info(JSON.stringify(hierarchy));
41+
``

0 commit comments

Comments
 (0)