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

try this code in a business rule as I mentioned earlier.


change name of variables as per need.



var flag = true;


  var gr = new GlideRecord("tasks");


  gr.addQuery("request", current.request);


  gr.addQuery('state','NOT IN','4,3'); // 3 closed complete & 4 closed incomplete


  gr.query();



  while(gr.next()){


  flag = false;


  }



if(flag){


  var gr3 = new GlideRecord("request");


  if(gr3.get(current.request)){



  var gr2 = new GlideRecord("sc_task");


  gr2.addQuery("request_item", current.request_item);


  gr2.addQuery('state', '3');


  gr2.query();


  if(gr2.next()){


  gr3.state = 3;


  }else{


  gr3.state = 4;


  }


  gr3.update();


  }


  }



Hope this will help you.


Hi Jotiram, thanks for the script. i will look into it tomorrow and let you know how it goes.



Hi Doug,


OK, so this is what I see:


gs.addInfoMessage(count + 'children found') yields all tasks, active and inactive.


ctTask.getRowCount() yields the active tasks, but ALL active tasks, not only those of the request itself. For example, if i have another request B with 3 tasks, and i close the tasks related to the specific request A (3 tasks), the request A still won't close because the script sees 3 other active tasks for request B, allbeit not related to request A itself.


If all tasks are closed in all requests, the request closes as soon as a field is updated and the request is saved.


In short, the script does not look at the specific tasks for the specific request - like no one-to-specific many relationship...



just for reference, this is the whole script, where myTask is the name of the tasks table and myRequest is the name of the request table:



var ctTask = new GlideRecord('myTasks');



ctTask.addQuery('myRequest', current.sys_id);


ctTask.query();



var count = ctTask.getRowCount();


gs.addInfoMessage(count + 'children found'); //--> this gives the total count of tasks


if (count > 0 ){


ctTask.addQuery('active',true);


ctTask.query();



if (!ctTask.next()) {


  answer = true;


  current.u_state = 3 //changes the state of the request to close complete if no active task


  current.update();


}


}



//just for information


var num = current.number;


gs.addInfoMessage("is "+ ctTask.getRowCount()); ////--> this gives the total count of active tasks, not just specific for the request


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


gs.addInfoMessage("is "+ num);


gs.addInfoMessage("is "+ ctReq);


vibhor_dwivedi
Mega Expert

you can try else in condition.



if (!NETask.next()) {  


  answer = true;


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


  req.update();


}


else


{


//Do nothing when there are no tasks.


}


Hi Vibhor,



Thanks, however, this does not help me see only the relevant tasks. The BR still refers to all open tasks for some reason, and not only to those related to the request itself, and therefore does not close the ticket.



harel


Hi Harel,




Try below script. It fetches all the tasks and checks state of all the tasks.




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('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