File tree Expand file tree Collapse file tree 1 file changed +41
-0
lines changed
Server-Side Components/Script Includes/Recursive GlideRecord Fetcher Expand file tree Collapse file tree 1 file changed +41
-0
lines changed Original file line number Diff line number Diff line change 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+ ``
You can’t perform that action at this time.
0 commit comments