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

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

Community Alums
Not applicable

Hi @Ankur Bawiskar ,

Thanks that worked