Loop through Nested Tasks to get parent task's title

rakesh241
Giga Contributor

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

 

4 REPLIES 4

DScroggins
Kilo Sage

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();




}

David Scroggins Code will work
 
To do this what you want to do is use a recursive function ( A function that calls itself until a condition is met )
 
Pseudo code for this would be 
 
// 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());
    }
}

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!

johansec
Tera Guru
David Scroggins Code will work
 
To do this what you want to do is use a recursive function ( A function that calls itself until a condition is met )
 
Pseudo code for this would be 
 
 var parent = getRootLevel(current record);
 
// Do what you want with the root level parent
gs.log(parent)
 
function getRootLevel(record){
   // Check if record has a parent if not it is a root level parent and return it
   if(records parent is null) {
     return record;
   }else{
      // Go to the next parent and have the function call itself to check the next record
       getRootLevel(record.parent);
   }
}
 
Hope this helps explain it some.