The CreatorCon Call for Content is officially open! Get started here.

Background Script to Close Open Requests, Requested Items, Tasks

Laurie Marlowe1
Kilo Sage

Hello,

We have a request to "cleanup" old Requests where some of the RITMs are still open because some of the Tasks were never completed.  I've got a background script working all except for closing the Tasks.

I've been given a list (a very long list) of the Request numbers that need to be cleaned up.  What is wrong with my script that I cannot get the tasks closed?  

        var scrs = ['SCR0111947', ]; //list of Requests that have open tasks - will get the list and format in Excel and paste into the script for the array

        for (var i = 0; i < scrs.length; i++) {
            var rec = new GlideRecord('sc_request');
            rec.addEncodedQuery('numberSTARTSWITH' + scrs[i]);
            rec.query();
            if (rec.next()) {
                rec.setWorkflow(false);
                rec.state = 4;
                rec.stage = 'closed_incomplete';
                rec.description = "Bulk closure of old tickets per SER0058621";
                rec.update();

                //find all the RITMs for each SCR

                var ritm = new GlideRecord('sc_req_item');
                ritm.addQuery('requestSTARTSWITH' + scrs[i]);
                ritm.addQuery('active', true);
                ritm.query();
                while (ritm.next()) {

                    ritm.setWorkflow(false);
                    ritm.state = 4;
                    ritm.stage = 'Request Cancelled';
                    ritm.update();
                }
                //find all the RTASKs for each SCR/RITM
				
                var rtask = new GlideRecord('sc_task');
                rtask.addQuery('request.numberSTARTSWITH' + scrs[i]);
                rtask.addQuery('active', true);
                while (rtask.next()) {
		    gs.print('SCR is ' + request.number);
                    rtask.setWorkflow(false);
                    rtask.state = 4;
                    rtaks.update();
                }
            }
        }
    

Any help is greatly appreciated! 

Thank you,

Laurie

1 ACCEPTED SOLUTION

Kieran Anson
Kilo Patron

You can condense this by using the updateMultiple function. Have you also considered cancelling the workflows/flows behind these records?

var SOURCE_REQUESTS = ['REQ0010027', 'REQ0010026','REQ0010011'];

var requestGR = new GlideRecord('sc_request');
requestGR.addQuery('number' , 'IN' , SOURCE_REQUESTS);
requestGR.setValue('state' , 4);
requestGR.setValue('request_state' , 'closed_incomplete');
requestGR.setValue('stage' , 'closed_incomplete');
requestGR.setValue('description' , 'Bulk closure of old tickets per SER0058621');
requestGR.setWorkflow(false);
requestGR.updateMultiple();

var requestItemGR = new GlideRecord('sc_req_item');
requestItemGR.addQuery('request.number' , 'IN' , SOURCE_REQUESTS);
requestItemGR.addActiveQuery();
requestItemGR.setValue('state' , 4);
requestItemGR.setValue('stage' , 'Request Cancelled');
requestItemGR.setWorkflow(false);
requestItemGR.updateMultiple();

var taskGR = new GlideRecord('sc_task');
taskGR.addQuery('request.number' , 'IN' , SOURCE_REQUESTS);
taskGR.addActiveQuery();
taskGR.setValue('state' , 4);
taskGR.setWorkflow(false);
taskGR.updateMultiple();

View solution in original post

8 REPLIES 8

Kieran Anson
Kilo Patron

You can condense this by using the updateMultiple function. Have you also considered cancelling the workflows/flows behind these records?

var SOURCE_REQUESTS = ['REQ0010027', 'REQ0010026','REQ0010011'];

var requestGR = new GlideRecord('sc_request');
requestGR.addQuery('number' , 'IN' , SOURCE_REQUESTS);
requestGR.setValue('state' , 4);
requestGR.setValue('request_state' , 'closed_incomplete');
requestGR.setValue('stage' , 'closed_incomplete');
requestGR.setValue('description' , 'Bulk closure of old tickets per SER0058621');
requestGR.setWorkflow(false);
requestGR.updateMultiple();

var requestItemGR = new GlideRecord('sc_req_item');
requestItemGR.addQuery('request.number' , 'IN' , SOURCE_REQUESTS);
requestItemGR.addActiveQuery();
requestItemGR.setValue('state' , 4);
requestItemGR.setValue('stage' , 'Request Cancelled');
requestItemGR.setWorkflow(false);
requestItemGR.updateMultiple();

var taskGR = new GlideRecord('sc_task');
taskGR.addQuery('request.number' , 'IN' , SOURCE_REQUESTS);
taskGR.addActiveQuery();
taskGR.setValue('state' , 4);
taskGR.setWorkflow(false);
taskGR.updateMultiple();

Hi Kieran,

Thank you for the suggestions to improve my coding!  I will do this going forward.  Also, thank you for the reminder about the cancelling the workflows.  I will give this a try on Monday.

Cheers,

Laurie

Laurie Marlowe1
Kilo Sage

Hi Kieran,

I tried to cancel the workflows, but this did not work.

var requestItemGR = new GlideRecord('sc_req_item');
requestItemGR.addQuery('request.number' , 'IN' , SOURCE_REQUESTS);
requestItemGR.addActiveQuery();
requestItemGR.setValue('state' , 4);
requestItemGR.setValue('stage' , 'Request Cancelled');
var workflow = new Workflow();
requestItemGR.setWorkflow(false);
var workflow = new Workflow();
workflow.cancel(requestItemGR);
requestItemGR.updateMultiple();

Is there a different way this needs to be coded since multiple records with active workflows will need to be cancelled?  Or do I need to create a new loop and cancel one at a time?

Thank you, your help is much appreciated!

Laurie

ShubhamGarg
Kilo Sage

Hi Laurie,

From the process standpoint, when you cancel the workflow the current and all remaining tasks will be cancelled.

Below links should help you here -

Cancel and restart workflow

Cancel a workflow

Cancel Workflow closes active tasks - that's bad

Mark this as Correct/Helpful if it helps.

Regards,

Shubham