Change state of request when all related tasks are closed

oharel
Kilo Sage

Hi guys,

 

The scenario is this: I created an application in which one of the UIs creates tasks. I would like the state of the request to automatically change to close complete once all tasks are closed. I created a business rule and sort of merged two scripts: from here and from here.

What happens is that as soon as the request is opened and any field is updated (and request saved) the request state turns to close complete. This is because the script checks the state of the related tasks and if they are not active, it closes the request. As there are no tasks at that stage, the request is closed...

I am looking for a way to overcome that. The script needs to check first if there are any tasks and only then check their state. If no tasks exist for the request, or the existing tasks are not closed, then just continue as usual and not change the state of the request.

 

Below is my script.

var req = new GlideRecord('request');  

var NETask = new GlideRecord('tasks');  

 

//Get the current request so we can update it, if necessary  

req.get(current.sys_id);  

 

//Run a query against all active request tasks related to this request

NETask.addQuery('request', current.sys_id);  

NETask.addQuery('active',true);

NETask.query();

 

if (!NETask.next()) {  

  answer = true;

  req.u_state = 3 //changes the state of the request to close complete

  req.update();

}

   

gs.addInfoMessage("is "&& NETask.getRowCount()); //this is just to see the number of open tasks count

gs.addInfoMessage("is "&& current.sys_id); // and the sys_id for basic debugging

 

Thanks

Harel

23 REPLIES 23

The Machine
Kilo Sage

Hello,



Are you using a workflow for your request/request item?   If so and you have multiple tasks for different groups to complete you could use the join activity in the workflow to join those tasks and set the "Stage" to complete when you reach the end.   This should activate the business rule to close the parent RITM/request.



Hope that helps!



Mike Bahr


Sorry, for got to mention I am not currently using a workflow. Script-based only.


No problem.   Just noticed in your var NETask = new GlideRecord('tasks'); is that a custom table or did you mean the "task" table.



Also is the task set to be the parent of the request upon creation.   If so, you can do an addQuery for parent, current.sys_id) in the task to look for the parent request and if found set the state to 3.



Hope that makes sense.   I would approach this in a workflow.   To me it would be easier to manage and scale down the line if needed, plus it uses the OOB functionality so you don't have to reinvent the wheel.



Completely understand if that is not on option for you.



Hopefully that helps.



Thanks!



Mike Bahr


marcguy
ServiceNow Employee
ServiceNow Employee

you can do hasNext() to check if a record has any children at all.



i.e.



if (!task.hasNext())


{


//do whatever you want to do if there arent any


}