Set parent task status based on the child task status

Community Alums
Not applicable

Hello Team,

 

I have a requirement where Parent task will have 9 child tasks, Parent task status should get set based on the child tasks status, for example if all 9 child tasks are in "closed completed" parent task status should be "approved", if any of the child task status is set to "closed incomplete" parent task status should be set as "conditional approved". I'm kind of stuck in building the logic for above scenario. I tried to create BR on task status change and get the all the child tasks count and stuck how to check the status of all the child tasks, please help in building the logic

 

Thanks,

Prudhvi  

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@Community Alums 

you should have after update BR on child task table and then use GlideAggregate to know the count

Condition: Parent IS NOT EMPTY AND State [IS ONE OF] Closed Complete OR Closed Incomplete

Script:

Ensure you use correct table name, field name, choice values to compare etc

(function executeRule(current, previous) {
    var parentSysId = current.parent;
    if (!parentSysId) 
        return;

    var parentTask = current.parent.getRefRecord();

    // Track closed complete and incomplete counts
    var closedComplete = new GlideAggregate(current.getTableName());
    closedComplete.addQuery('parent', parentSysId);
    closedComplete.addQuery('state', 'closed_complete');
    closedComplete.addAggregate('COUNT');
    closedComplete.query();
    var closedCompleteCount = closedComplete.next() ? parseInt(closedComplete.getAggregate('COUNT')) : 0;

    var closedIncomplete = new GlideAggregate(current.getTableName());
    closedIncomplete.addQuery('parent', parentSysId);
    closedIncomplete.addQuery('state', 'closed_incomplete');
    closedIncomplete.addAggregate('COUNT');
    closedIncomplete.query();
    var closedIncompleteCount = closedIncomplete.next() ? parseInt(closedIncomplete.getAggregate('COUNT')) : 0;

    // Total child tasks
    var totalChildTasks = new GlideAggregate(current.getTableName());
    totalChildTasks.addQuery('parent', parentSysId);
    totalChildTasks.addAggregate('COUNT');
    totalChildTasks.query();
    var totalCount = totalChildTasks.next() ? parseInt(totalChildTasks.getAggregate('COUNT')) : 0;

    // Determine parent status
    var newParentState = '';
    if (closedIncompleteCount > 0) {
        newParentState = 'conditional_approved';
    } else if (closedCompleteCount === totalCount) {
        newParentState = 'approved';
    }

    // Update parent if needed
    if (newParentState && parentTask.state != newParentState) {
        parentTask.setValue('state', newParentState);
        parentTask.update();
    }
})(current, previous);

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

6 REPLIES 6

SanjivMeher
Kilo Patron
Kilo Patron

You need a BR or a Flow Designer.

Create an onAfter BR with condition to run only when Parent is not empty and active changes to false.

And then add a script

 

For ex

 

// Are there any active child records
var ch = new GlideRecord(current.sys_class_name);
ch.addQuery('parent',current.getValue('parent'));
ch.addQuery('active','true');
ch.query();

if (!ch.next())
{
// if none found, close the parent
var pr = new GlideRecord(current.parent.sys_class_name);
pr.addQuery('sys_id',current.getValue('parent'));
pr.query();]
if (pr.next())
{
pr.state = '3'; //Closed Complete
pr.work_notes = "Auto-closed as all the child tasks are closed";
pr.update();
}
}

 

 


Please mark this response as correct or helpful if it assisted you with your question.

Chaitanya ILCR
Kilo Patron

Hi @Community Alums ,

(function executeRule(current, previous /*null when async*/ ) {
    // Get the parent task
    var parentField = 'parent'; //replace parent with parent field name if it's different
    var parentTask = current[parentField].getRefRecord();


    var childTaskGR = new GlideRecord('child_task_table'); // Replace with your child task table name
    childTaskGR.addQuery(parentField, current.getValue(parentField)); //replace parent with parent field name if it's different
    childTaskGR.query();


    parentTask.status = 'approved';

    while (childTaskGR.next()) {
        if (childTaskGR.active)   // if any of the child taks are stil active leaving parent task status as is
            return;

        if (childTaskGR.status == 'closed_incomplete') {
            parentTask.status = 'conditional_approved';
            break;
        }
    }

    parentTask.update();

})(current, previous);

 

have an after update BR on the child task table where status changes to closed complete or closed incomplete

 

update the script's table names and field names as per your requirement

 

Please mark my answer as helpful/correct if it resolves your query.

Regards,
Chaitanya

 

 

Community Alums
Not applicable

Hi @Chaitanya ILCR ,

 

The code you shared is getting the state on child task but I want based on child task parent task state should get updated when all the 9 child task completed parent task should be approved, if any one of the task is in incomplete state the parent state should be in conditional approved

DivyanshiS
Tera Contributor

Please share the script you have already written and highlight the part you are struggling with.