Background Script to Close Completed Tickets

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-28-2017 09:41 AM
Long before I arrived, there was a horde of tickets that did not close, even though the the RITMs were completed. We found the problem in the workflows (several of them) and corrected them. But that still left me with almost 2,000 tickets to close.
I decided to use a Script Includes run in a Background Script.
Since the stage of all the ones effected was set to completed, but not closed - seemed pretty easy.
Then we decided we should make sure all of the tasks were completed as part of the criteria. There are well over a dozen workflow types that I would need the name and tasks that could be generated. As I went through each workflow and counted the tasks for each request, I found there were two primary states that differentiated how many tasks: approval= rejected or approved=approved. Then I came across a third, the request was rejected, then approved after a task was created to fix an issue - Different numbers of tasks for approved state. Thus, I decided to add a loop to count the total tasks and the completed tasks - if they were equal AND the stage was Complete, close the Requirement.
To test, I added an action parameter. If action is not set - it is automatically set to 0 - this would report which Requests will be affected, but not delete them.
I came up with this - ran it on the Dev Server and then the Test Server before moving it to Production:
closeRequests Script Includes
Useage: var cr = new closeRequests(reqState, action);
reqState use the following: 1 - Open, 2 - Work in Progress, -5 - Pending, if not set, defaults to 1.
action- Generate Report: (anything), Set Request to CLOSED: 1
var closeRequests = Class.create();
closeRequests.prototype = {
initialize: function(reqState, action) {
if(reqState != 1 || reqState != '1')
{
reqState = 0;
}
var i = 0;
var j = 0;
var k = 0;
var reqitem = new GlideRecord('sc_req_item');
reqitem.addQuery('state', reqState);
reqitem.addQuery('stage', 'Completed');
reqitem.query();
while(reqitem.next())
{
i++;
//Check for and tasks where they are closed
var SCTask = new GlideRecord('sc_task');
SCTask.addQuery('request_item', reqitem.sys_id);
SCTask.query();
var ttsk = 0;
var ctsk = 0;
while (SCTask.next())
{
if(SCTask.state == 3|| SCTask.state ==4 || SCTask.state ==7)
{
ctsk++;
}
ttsk++;
}
if(ctsk == ttsk && action==1 &&ttsk>0)
{
gs.print('All Tasks closed - closing Item: '+reqitem.number);
reqitem.state = 3;
reqitem.update();
j++;
}
else if(ctsk == ttsk && action==0 && ttsk>0)
{
gs.print('All Tasks Closed for: '+reqitem.number);
j++;
}
else
{
k++;
}
}
gs.print('Items For State: '+reqState+', Total found:'+i+'/'+j+'/'+k);
type: 'closeRequests'
}
}
Any feedback on enhancing this would be welcome.
Mike C
- Labels:
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-28-2017 09:57 AM

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-28-2017 11:23 AM
Thx!
MC