Skip to content

Commit b47ca16

Browse files
authored
Merge pull request #2170 from shivamvish160/recursive
Recursive GlideRecord Fetcher
2 parents 91d5840 + 4040096 commit b47ca16

File tree

2 files changed

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

2 files changed

+70
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Recursive GlideRecord Fetcher
2+
## Overview
3+
This snippet provides a reusable logic to recursively fetch child records from a parent record in ServiceNow. It is useful for traversing hierarchical relationships such as tasks, categories, CMDB CI relationships, or any table with a parent-child structure.
4+
5+
The logic prevents infinite loops by tracking visited records and supports nesting of children for structured output.
6+
7+
---
8+
9+
## Use Case
10+
- Fetch all subtasks under a parent task.
11+
- Traverse CMDB CI relationships recursively.
12+
- Build hierarchical views of organizational units or categories.
13+
14+
---
15+
16+
## Parameters
17+
| Parameter | Description |
18+
|------------------|-----------------------------------------------------------------------------|
19+
| `tableName` | Name of the table to query (e.g., `task`, `cmdb_ci`, `custom_table`) |
20+
| `parentField` | Field that links to the parent record (e.g., `parent`, `parent_id`) |
21+
| `parentSysId` | Sys ID of the root parent record to start traversal |
22+
23+
---
24+
25+
## Example Usage
26+
```javascript
27+
var fetcher = new RecursiveFetcher('task', 'parent');
28+
var hierarchy = fetcher.fetchChildren('abc123sysid'); // Replace with actual parent sys_id
29+
gs.info(JSON.stringify(hierarchy));
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)