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

Not sure about the custom tables.I tried attached script for OOTB sc_task and sc_req_item table and it worked for me.


Hi Vibhor,



I have a feeling the problem is that although the task is created , it does not have a parent, which might explain why, even though we're trying to tell the script to sift through the task and relate only to the tasks that have the parent's sys_id, it does not work. I also added an info message:


gs.addInfoMessage("Tasks created number " + task.number + " and the task parent sys_id is " + task.current.parent.sys_id);


the answer i get is:


Tasks created number NETask0001409 and the task parent sys_id is undefined



Just for reference, here the script that creates the tasks (where u_new_request_task is the task template):



insertNeTasks();



function insertNeTasks() {


  var template = new GlideRecord('u_new_request_task');


  var localITGroup = current.u_customer.location.u_local_it_group.sys_id;


  template.addQuery('myRequest', current.number);


  template.query();



  while(template.next()) {


  var task = new GlideRecord('myTask');


  task.initialize();


  task.u_ne_request = current.number;


  task.u_employee_name = current.u_employee_name;


  task.short_description = template.u_short_description;


  task.description = template.u_description;


  task.assignment_group = localITGroup;



  task.insert();



  gs.addInfoMessage("Tasks created number " + task.number + " and the task parent sys_id is " + task.parent.sys_id);


  action.setRedirectURL(current);



  }


}


Harel,


Need some info from you,


"myTask" this is your task table. Do you have any field on this table which is pointing to "u_new_request_task" table?



If yes, then I have made one change in your code(in bold) assuming "u_ne_request" is a field referencing to "u_new_request_task".



insertNeTasks();



function insertNeTasks() {


  var template = new GlideRecord('u_new_request_task');


  var localITGroup = current.u_customer.location.u_local_it_group.sys_id;


  template.addQuery('myRequest', current.number);


  template.query();



  while(template.next()) {


  var task = new GlideRecord('myTask');


  task.initialize();


  task.u_ne_request = current.number;


  task.u_employee_name = current.u_employee_name;


  task.short_description = template.u_short_description;


  task.description = template.u_description;


  task.assignment_group = localITGroup;



  task.insert();



  gs.addInfoMessage("Tasks created number " + task.number + " and the task parent sys_id is " + task.u_ne_request);


  action.setRedirectURL(current);



  }


}



and if you don't have any field on "myTask" table referencing to "u_new_request_task", I think you need to create one.



Thanks.


Hi Jotiram,



Thanks for the help so far!


I created the field as I did not have one. I created it as reference field, though.


adding task.u_ne_request (that is the name of the field) yields nothing though.


I think I messed up something when creating the tables, or am missing some fundamental link between them...