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

Hi Vibhor,



results are the same - request closes if there are no existing tasks to it.



Every task has a field which shows the request it belongs to . When creating the task, the script adds:


task.u_ne_request = current.number; // the field u_ne_request gets the value from the request's number



OK, so I was wondering: would it be possible to add a condition that says: look only for tasks that have u_ne_request = current number. if there are no tasks like that - ignore. if there are, check their state. if state is not closed - ignore. if it is - close request.


does that make sense?


I have updated the query condition in below script with "NETask.addQuery('u_ne_request', current.sys_id);"




In below script we are checking states based on OOTB task table states.Not sure if you have added any states for custom task table.




var req = new GlideRecord('request');


var NETask = new GlideRecord('tasks');


var flag=false;


//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('u_ne_request', current.sys_id);


NETask.addQuery('active',true);


NETask.query();




//If task exist then traverse through all the tasks to check state.. else just do nothing.Removed ! condition.




if (NETask.getRowCount()>0)


  {


    while(NETask.next())


  {


    //Check tasks in any of the following state is in Open(1),Pending(-5), InProgress(30), Responded(20), New(10).


   


    if((NETask.state == 30) || (NETask.state == 😎 || (NETask.state == 10) || (NETask.state == 1) || (NETask.state == 20))


  {


  flag = true;


  }


  }


}


else


  {


    //Do nothing


}



// If flag is false. it means all tasks are in closed or closed incomplete state.so request can be close.


if (!flag)


  {


  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


Have you tried the one I shared... It should work. I am using the same piece of code and is working fine.


Hi Jotiram,



sorry for the delayed answer. Yes - i have tried it. I have a bit of trouble adapting it to my needs, though. Here is why:


i am using only two tables for this new application:


myRequest - the request itself. state field is called u_state.


myTask - the new tasks table; states are similar to the original task table.



I am not sure what to do with


var gr2 = new GlideRecord("sc_task"); --> is this the same as the "task" table which appears on the second line of the script you shared?


  gr2.addQuery("request_item", current.request_item); --> i dont have a requested item, just a request.



if i change the variable to reflect my request tables and task fields, nothing really happens, so it must be something i am not doing properly...





I am using the OOTB task table states, so that's not a problem.


The addition you made definitely narrows down the row count of the request.


I am using When: after + Update for this BR


this enables me to open the request and update one field without the request closing. as soon as I update another field, or create tasks and then update a field, the request shows that row count is 0, and it closes, even if the related tasks are in state "open".



Tough one...