Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions get_ancestor_tasks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* getAncestorTasks — Return an array of parent task records up the chain.
* @param {String} tableName — the name of the task table (e.g. “incident”)
* @param {String} sysId — sys_id of the starting record
* @param {Number} maxDepth — optional maximum depth to prevent infinite loops
* @returns {GlideRecord[]} — array of ancestor GlideRecord objects, ordered from immediate parent up to root
*/
function getAncestorTasks(tableName, sysId, maxDepth = 10) {
var ancestors = [];
var seen = {};
var depth = 0;

var gr = new GlideRecord(tableName);
if (!gr.get(sysId)) {
return ancestors; // no starting record
}

while (gr.parent && gr.parent.toString() && depth < maxDepth) {
var parentId = gr.parent.toString();
if (seen[parentId]) {
gs.warn("getAncestorTasks: detected cycle at " + parentId);
break;
}
seen[parentId] = true;

var parentGr = new GlideRecord(tableName);
if (!parentGr.get(parentId)) {
gs.warn("getAncestorTasks: parent record not found: " + parentId);
break;
}
ancestors.push(parentGr);
gr = parentGr;
depth++;
}
return ancestors;
}
Loading