Change state of request when all related tasks are closed
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-12-2014 07:14 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-12-2014 08:09 AM
I am assuming you are referring to the catalog task(sc_task) table. If that is so, then why can't you have a business rule running after update on sc_task table.
You can have condition on business rule to run when task state is closed complete.
If condition matches, have the piece of code to close request associated with that task.
Will it help ?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-12-2014 01:26 PM
Hi all,
Thanks for the quick replies!
Mike - yes, it is a custom table. If I understand you correctly, then it is the opposite - the request is the parent of the tasks (more than one task). Also, workflows are not an option currently.
Jotiram - it is a custom task table, but as there's more than one task, it wouldn't do to change the state of the request when only one of them closes. That is why I am looking for a way to check each of them and if they are all closed, then and only then to close the request.
mguy - thanks. Tried that. The problem is that there is no actual check if there are any existing tasks before closing the request. That is - no task is like 10 non-active close completed tasks, so as soon as something changes in the request, even without related tasks, the request closes...
Actually, I think I am looking for a script that would say:
first, check if there are any related tasks, in whatever state - inactive or active
second, check if all of them are closed
third, if they are all closed, close the request
The first and second conditions elude me
Thanks,
Harel
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-12-2014 01:33 PM
I believe you can use a getRowCount() to find out if there are children or not.... documentation on it is here...
Coding Best Practices - ServiceNow Wiki
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-12-2014 02:03 PM
Thanks Doug, tried that.
getRowCount() relates to active tasks, seems like, because it shows "0" when the tasks are closed. In the original script, the one-before-last line is
gs.addInfoMessage("is "&& NETask.getRowCount());
I wanted to see, like what you suggested, if I could have the script work only if there were more than 0 tasks related to the request. As it happens, each time I closed a task, the number decreased by 1, and when all related tasks were closed, the number was 0. This is also true when the requested is initially created - 0 tasks. And therefore the request closes automatically... that is why i am asking if there is a way to check both active and inactive tasks before running the script. As a condition to the BR, for example.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-12-2014 02:21 PM
yep you are going to have to wrap the whole thing in a row count in a separate query you are getting a row count of zero because you have inactive in the query.
var ctTask = new GlideRecord('tasks');
ctTask.addQuery('request', current.sys_id);
ctTask.query();
var count = ctTask.getRowCount();
gs.addInfoMessage(count + 'children found');
if (count > 0 ){put your script here}