close parent after closing all child project tasks

Michael51
Tera Guru

Hello All , 

 

I have an issue with this below logic  , basically requirement is when ever we submit an demand 2 project tasks gets created , so now issue is when ever we close the single project task and the other one is open , parent ticket is getiing completed( state is moving to completed) , I have written BR to achieve this but it is updating demand soon as I just complete single task  , can someone help me here

 

 var count = 0;

        var ga = new GlideAggregate('pm_project_task');
        ga.addQuery('sys_id', current.parent);
        ga.addQuery("active", true);
        ga.addAggregate("COUNT");
        ga.query();

        if (ga.next()) {
            count = ga.getAggregate("COUNT");
        }
        if (count == 0) {

            var gr = new GlideRecord('dmn_demand');
            gr.addQuery('sys_id', current.parent);
            gr.query();
            while (gr.next()) {


                gr.state = 9;

                gr.update();
            }
        }

 

 

@Ankur Bawiskar  ,  @Pavankumar_1   

2 REPLIES 2

DUGGI
Giga Guru

@Michael51 

I understand that you want to update the parent demand to 'Completed' when all project tasks are closed.

 

*** ga.addQuery("state", "!=", 4); // Assuming state 4 means closed; update this number accordingly

 

var count = 0;

// Query for active project tasks with the same parent as the current task
var ga = new GlideAggregate('pm_project_task');
ga.addQuery('parent', current.parent);
ga.addQuery("state", "!=", 4); // Assuming state 4 means closed; update this number accordingly
ga.addAggregate("COUNT");
ga.query();

if (ga.next()) {
    count = ga.getAggregate("COUNT");
}

// If no active project tasks are found, update the parent demand
if (count == 0) {
    var gr = new GlideRecord('dmn_demand');
    gr.addQuery('sys_id', current.parent);
    gr.query();
    if (gr.next()) {
        gr.state = 9;
        gr.update();
    }
}

In this script, I've updated the GlideAggregate query to count project tasks with the same parent as the current task and a state other than closed (assuming state 4 means closed). If there are no such tasks, it will update the parent demand to 'Completed' (state 9).

 

Please update the state numbers according to your instance if they are different. This script should now work as expected and update the parent demand only when all project tasks are closed.

Pavankumar_1
Mega Patron

Hi @Michael51 ,

First you need to identify the functionality how it is parent getting close once you close single task.

Then comment that or make it inactive. 

Once it is inactive you can implement your logic and use below script on after business rule n pm_project_task table.

 

(function executeRule(current, previous /*null when async*/ ) {
    var ga = new GlideAggregate('pm_project_task');
    ga.addQuery('parent', current.parent);
    ga.addQuery("active", true);
    ga.query();
    if (!ga.next()) { //if we don't have any acive records then close parent
        var gr = new GlideRecord('dmn_demand');
        gr.addQuery('sys_id', current.parent);
        gr.query();
        if (gr.next()) {
            gr.state = 9;
            gr.update();
        }
    }
})(current, previous);

 

If it helps please click Accept as Solution/hit the Thumb Icon.
ServiceNow Community MVP 2024.
Thanks,
Pavankumar