Get state value of child tasks

Ben42
Tera Contributor

Hello,

 

I have 2 child tasks created under a parent record. And two tasks are of two different “Type”s. Now I wan to get the state value of those two tasks.. how can I get that? 

based on the state values of those two tasks, I need to set field on parent record. It’s not just when two child tasks state is closed then close parent.. we have other combinations we need to check for state fields on both child tasks.. 

 

when state on two child tasks are closed then close parent.

when state on one task is closed and other one is pending then set state on parent to different value.

Any help with script is much appreciated!

Thanks,

Ben

6 REPLIES 6

Jagadish Sanadi
Kilo Sage

Hello @Ben42 

 

Please provide parent and child table names.

Hi Jadadish,

 

It’s our custom tables but Consider them as Incident and Incident task table. 

thanks,

Ben.

Hello @Ben42 

 

var child = new GlideRecord('incident');
child.addQuery('parent', current.parent);
child.query();
while (child.next()) {
var type= child.type; 
gs.print(type);

}

If my response proves useful, please indicate its helpfulness by selecting "Accept as Solution" and " Helpful." 

 

 

SP22
Mega Sage
Mega Sage

Hi @Ben42,

Please find the below sample code and let me know whether it is useful or not.

 

(function executeRule(current, previous /* previous is only available in before business rules */) {

    // Query for child tasks related to the parent record
    var childTaskGr = new GlideRecord('child_task_table_name');
    childTaskGr.addQuery('parent', current.parent); // Assuming 'parent' is the field linking child tasks to the parent record
    childTaskGr.query();

    var allClosed = true;
    var atLeastOneClosed = false;

    // Loop through child tasks
    while (childTaskGr.next()) {
        var state = childTaskGr.state; // Assuming 'state' is the field storing the state of the child task

        // Check state conditions
        if (state != 'closed') {
            allClosed = false;
        } else {
            atLeastOneClosed = true;
        }
    }

    // Update parent record based on conditions
    var parentGr = new GlideRecord('parent_table_name');
    if (atLeastOneClosed && allClosed) {
        parentGr.get(current.parent);
        parentGr.state_field = 'Closed'; // Update parent state field
        parentGr.update();
    } else if (/* other conditions */) {
        // Handle other conditions as needed
    }

})(current, previous);

 

 

Thanks
SP