Fix script to close the REQ RITM SCTASK

Rakesh40
Tera Contributor

Hello All,

 

Can please someone help me with the Fix script to close the REQ RITM SCTASK records which are active and updated before one year ago.

i have below encoded query

active=true^sys_updated_on<javascript&colon;gs.beginningOfOneYearAgo()

 

 

Thanks

1 ACCEPTED SOLUTION

Robbie
Kilo Patron
Kilo Patron

Hi @Rakesh40,

 

So my update doesn't get lost in the thread.

Updated script below - I forgot to un-comment the update of setting any 'Requested' approvals to 'Cancelled'

 

As stated previous, please cherry pick a Request which has the necessary Request Item(s) and SC Task(s) to ensure you are happy before running against the whole result set.

You'll see I've done this one line 2. When you are ready, un-comment line 1, and comment line 2.

 

To help others (or for me to help you more directly), please mark this response correct by clicking on Accept as Solution and/or Kudos.

Thanks, Robbie

 

//var encodedQuery = 'active=true^sys_updated_on<javascript&colon;gs.beginningOfLast12Months()';
var encodedQuery = 'active=true^sys_updated_on<javascript&colon;gs.beginningOfLast12Months()^number=REQ0010006';
var requestGR = new GlideRecord('sc_request');
requestGR.addEncodedQuery(encodedQuery);
requestGR.query();
while (requestGR.next()) {
    //Loop through Requests
    requestGR.setValue('state', 4);
    requestGR.setValue('request_state', 'closed_incomplete');
    requestGR.setValue('stage', 'closed_incomplete');
    requestGR.setValue('close_notes', 'Bulk closure of old tickets per XYZ requirement'); //It's always best to add a comment for a bulk closure
    requestGR.setWorkflow(false);
    requestGR.update();
    //gs.print('Request: ' + requestGR.number);

    //Loop through Approvals for Request
    var requestApprovalsGR = new GlideRecord('sysapproval_approver')
    requestApprovalsGR.addQuery('sysapproval.sys_id', requestGR.sys_id);
    requestApprovalsGR.addQuery('state', 'requested');
    requestApprovalsGR.query();
    while (requestApprovalsGR.next()) {
        requestApprovalsGR.setValue('state', 'cancelled');
		requestApprovalsGR.update();
		//gs.print('Request Approvals: ' + requestApprovalsGR.sysapproval.number);
    };

    //Loop through Request Items
    var requestItemGR = new GlideRecord('sc_req_item');
    requestItemGR.addQuery('request.sys_id', requestGR.sys_id);
    requestItemGR.query();
    while (requestItemGR.next()) {
        requestItemGR.addActiveQuery();
        requestItemGR.setValue('state', 4);
        requestItemGR.setValue('stage', 'Request Cancelled');
        requestItemGR.setValue('close_notes', 'Bulk closure of old tickets per XYZ requirement'); //It's always best to add a comment for a bulk closure
        requestItemGR.setWorkflow(false);
        requestItemGR.update();
        //gs.print('Request Items: ' + requestItemGR.number);

        //Loop through Approvals for Request Item
        var requestItemApprovalsGR = new GlideRecord('sysapproval_approver')
        requestItemApprovalsGR.addQuery('sysapproval.sys_id', requestItemGR.sys_id);
        requestItemApprovalsGR.addQuery('state', 'requested');
        requestItemApprovalsGR.query();
        while (requestItemApprovalsGR.next()) {
            requestItemApprovalsGR.setValue('state', 'cancelled');
			requestItemApprovalsGR.update();
			//gs.print('Request Item Approvals: ' + requestItemApprovalsGR.sysapproval.number);
        };

        //Loop through Tasks
        var taskGR = new GlideRecord('sc_task');
        taskGR.addQuery('request_item.sys_id', requestItemGR.sys_id);
        taskGR.query();
        while (taskGR.next()) {
            taskGR.addActiveQuery();
            taskGR.setValue('state', 4);
            taskGR.setValue('close_notes', 'Bulk closure of old tickets per XYZ requirement'); //It's always best to add a comment for a bulk closure
            taskGR.setWorkflow(false);
            taskGR.update();
            //gs.print('SC Tasks: ' + taskGR.number);
        }
    }
}

 

View solution in original post

12 REPLIES 12

Hi @Rakesh40,

 

I'm a little confused, do you need to approve the approvals for these records, or wish to cancel them?

 

You've also triggered a thought process in my head - depending on the Catalog Items, you should consider cancelling the underlying Workflow for these Requests a well so as to make sure everything is closed out.

 

To help others (or for me to help you more directly), please mark this response correct by clicking on Accept as Solution and/or Kudos.



Thanks, Robbie

Rakesh40
Tera Contributor

Hello @Robbie 

Sorry I want to gets the approvals cancelled.

 

SetWorkflow(false) this won't cancel the complete executions ??

 

Hi @Rakesh40,

 

Updated Script to cater for Approval at both Request and Request Item level.

As stated in on of my previous posts, please also consider closing out active Workflow associated to the Request/Request Items.

 

Updated Script:

//var encodedQuery = 'active=true^sys_updated_on<javascript&colon;gs.beginningOfLast12Months()';
var encodedQuery = 'active=true^sys_updated_on<javascript&colon;gs.beginningOfLast12Months()^number=REQ0010006';
var requestGR = new GlideRecord('sc_request');
requestGR.addEncodedQuery(encodedQuery);
requestGR.query();
while (requestGR.next()) {
    //Loop through Requests
    requestGR.setValue('state', 4);
    requestGR.setValue('request_state', 'closed_incomplete');
    requestGR.setValue('stage', 'closed_incomplete');
    requestGR.setValue('close_notes', 'Bulk closure of old tickets per XYZ requirement'); //It's always best to add a comment for a bulk closure
    requestGR.setWorkflow(false);
    //requestGR.update();
    gs.print('Request: ' + requestGR.number);

    //Loop through Approvals for Request
    var requestApprovalsGR = new GlideRecord('sysapproval_approver')
    requestApprovalsGR.addQuery('sysapproval.sys_id', requestGR.sys_id);
    requestApprovalsGR.addQuery('state', 'requested');
    requestApprovalsGR.query();
    while (requestApprovalsGR.next()) {
        requestApprovalsGR.setValue('state', 'cancelled');
		//requestApprovalsGR.update();
		gs.print('Request Approvals: ' + requestApprovalsGR.sysapproval.number);
    };

    //Loop through Request Items
    var requestItemGR = new GlideRecord('sc_req_item');
    requestItemGR.addQuery('request.sys_id', requestGR.sys_id);
    requestItemGR.query();
    while (requestItemGR.next()) {
        requestItemGR.addActiveQuery();
        requestItemGR.setValue('state', 4);
        requestItemGR.setValue('stage', 'Request Cancelled');
        requestItemGR.setValue('close_notes', 'Bulk closure of old tickets per XYZ requirement'); //It's always best to add a comment for a bulk closure
        requestItemGR.setWorkflow(false);
        //requestItemGR.update();
        gs.print('Request Items: ' + requestItemGR.number);

        //Loop through Approvals for Request
        var requestItemApprovalsGR = new GlideRecord('sysapproval_approver')
        requestItemApprovalsGR.addQuery('sysapproval.sys_id', requestItemGR.sys_id);
        requestItemApprovalsGR.addQuery('state', 'requested');
        requestItemApprovalsGR.query();
        while (requestItemApprovalsGR.next()) {
            requestItemApprovalsGR.setValue('state', 'cancelled');
			//requestItemApprovalsGR.update();
			gs.print('Request Item Approvals: ' + requestItemApprovalsGR.sysapproval.number);
        };

        //Loop through Tasks
        var taskGR = new GlideRecord('sc_task');
        taskGR.addQuery('request_item.sys_id', requestItemGR.sys_id);
        taskGR.query();
        while (taskGR.next()) {
            taskGR.addActiveQuery();
            taskGR.setValue('state', 4);
            taskGR.setValue('close_notes', 'Bulk closure of old tickets per XYZ requirement'); //It's always best to add a comment for a bulk closure
            taskGR.setWorkflow(false);
            //taskGR.update();
            gs.print('SC Tasks: ' + taskGR.number);
        }
    }
}

Please be careful with setWorkflow false, as this can cause Workflows/Flows/etc not to proceed, ending up with unnecessary other open records!

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn

Thanks @Mark Roethof - Noted and this is purposeful due to the activity and not to fire out emails etc. You'll notice I mentioned about handling the flow etc within the operation

 

To help others (or for me to help you more directly), please mark this response correct by clicking on Accept as Solution and/or Kudos.



Thanks, Robbie