How to auto-populate time_worked field for parent project task with time worked from child tasks

MBarrott
Mega Sage

Is there a way for the time_worked values of all child project tasks to be summed and auto-populate the time_worked field on the parent project task?

 

 

1 ACCEPTED SOLUTION

maheshkn
Mega Guru

Write an After Insert and Update Business Rule on the Project Task table with the condition 'Time Worked changes.

 

Use the below script:

 

  var totalTimeworked = 0;
    if (current.parent) {
        var taskTimeWorkedGa = new GlideAggregate("pm_project_task");
        taskTimeWorkedGa.addQuery("parent", current.parent);
        taskTimeWorkedGa.addAggregate("SUM", "time_worked");
        taskTimeWorkedGa.groupBy('parent');
        taskTimeWorkedGa.query();
        if (taskTimeWorkedGa.next()) {
            totalTimeworked = taskTimeWorkedGa.getAggregate("SUM", "time_worked");

        }

        var parentRec = current.parent.getRefRecord();
        parentRec.setValue('time_worked', totalTimeworked);
        parentRec.update();

    }
 
 
Result: Whenever the Time Worked field changes on child tasks, it will update the Time Worked field on the parent task.

 

View solution in original post

6 REPLIES 6

maheshkn
Mega Guru

Write an After Insert and Update Business Rule on the Project Task table with the condition 'Time Worked changes.

 

Use the below script:

 

  var totalTimeworked = 0;
    if (current.parent) {
        var taskTimeWorkedGa = new GlideAggregate("pm_project_task");
        taskTimeWorkedGa.addQuery("parent", current.parent);
        taskTimeWorkedGa.addAggregate("SUM", "time_worked");
        taskTimeWorkedGa.groupBy('parent');
        taskTimeWorkedGa.query();
        if (taskTimeWorkedGa.next()) {
            totalTimeworked = taskTimeWorkedGa.getAggregate("SUM", "time_worked");

        }

        var parentRec = current.parent.getRefRecord();
        parentRec.setValue('time_worked', totalTimeworked);
        parentRec.update();

    }
 
 
Result: Whenever the Time Worked field changes on child tasks, it will update the Time Worked field on the parent task.

 

@maheshkn this worked perfectly. Greatly appreciate the assistance on this. 

 

Accepted solution.