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

OlaN
Giga Sage
Giga Sage

Hi,

A script that does this isn't very hard to produce, but before you do go ahead with something like this, there are a couple of things to consider.

Should you update all records, just on the basis of when they where updated and they still are active?

There might be approvals that are pending, which has caused the REQ/RITM/SCTASK to wait.

If the records are updated, is there any automation/integration affected by the update? If yes, do you still want to update?

Do you want to update the records and set a new last updated date or not?

Do you want business rules to run on the updates or not?

If you close the SCTASKs is there any processing that closes the RITM automatically?

If you close the RITM, usually the REQ is closed automatically when all attached RITM are completed.

Rakesh40
Tera Contributor

Hi @OlaN ,

Yes i want to close all the active records which are still active since long time.

For few RITM records they're pending group approvals where SCTASKS yet to be created. But still I want to close the RITM.
Yes  i want to update the records and set a new last updated date.

i don't want to run the BR's

For auto closure of RITM and REQ we have generic flow to close once If we close all the SCTASKs.

 

 

Robbie
Kilo Patron
Kilo Patron

Hi @Rakesh40,

 

Please find the below Script that can be ran as a Fix Script.

Please note this is based on the assumption that you want to close all active Requests that haven't been updated for over a year, regardless of whether the underlying Request Item or SC Task have been updated.

 

Please consider any workflow etc associated to any Request/Request Items - these should also be 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

 

//var encodedQuery = 'active=true^sys_updated_on<javascript&colon;gs.beginningOfLast12Months()'; //Uncomment this once your happy with results against a test request as used below
var encodedQuery = 'active=true^sys_updated_on<javascript&colon;gs.beginningOfLast12Months()^number=REQ0010027';
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 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 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);
		}
	}
}

 

 

 

 

Rakesh40
Tera Contributor

Hello @Robbie 

For few RITM records they're pending group approvals where SCTASKS yet to be created. Before Closure of RITM the approvals needs to be approved.

How can i achieve this??