Loop through Nested Tasks to get parent task's title
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-21-2019 10:39 AM
I have a Project and and Tasks nested under this to multiple levels (A project may have many levels of child tasks). Requirement is "to a field X on the final child task in the heirarchy I have to setValue the title of Parent Task" . The structure looks something like: Project>ParentTask>ChildTask>ChildOfChildTask . So here set the title of the ParentTask to field X on ChildOfChildTask . There is a field called Parent on the task form, so the ParentTask in the above example will have Project in Parent field, where as the ChildTask has ParentTask in Parent field.
In real time a project may have any level of child task, so the challenge is to loop through till we find the parent task in the runtime. Any help with the code is much appreciated.
Thanks,
Rakesh
- Labels:
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-21-2019 11:05 AM
Hello,
Please try the following. This assumes you will use in a before update BR. Replace 'gr.x' with the actual field name for parent title on child task:
getParentTask(current.parent , current.sub_tree_root);
function getParentTask(parentId , rootId){
var gr = new GlideRecord('pm_project_task');
gr.get(parentId);
if(gr.parent != rootId){
var parentTitle = getParentTask(gr.parent , current.sub_tree_root);
}else{
return gr.getValue('short_description');
}
gr.x = parentTitle;
gr.update();
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-21-2019 11:39 AM
// This is the variable to hold the desired root level parent
var rootParent = getRootParent(current.sys_id.toString());
// Now do what you want with the root level record
gs.info(rootParent.sys_id.toString());
// This function will check to see if the record is a root level record
function getRootParent(rec) {
var record = new GlideRecord('YOUR TABLE NAME');
record.get(rec);
// If the record does not have a parent it is a root level record and return it
if (record.parent.toString() == '') {
return record;
} else {
// This is not a root level record so get the parent and
// Have the function call itself to check the next level
getRootParent(record.parent.toString());
}
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-25-2019 08:17 AM
Hi Rakesh,
Have you had a chance to try the above code? If you found it to be helpful or correct please mark as such so that others can benefit.
Thanks!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-21-2019 11:31 AM