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

Andrew_TND
Mega Sage
Mega Sage

Hi @Rakesh40 

Instead of running a fix script have you tried running a flow instead? This can be run on a scheduled basis, potentially future proof the process and most importantly reduce technical debt. 

Please mark as helpful or if its resolved the issue, CORRECT!

Sumanth16
Kilo Patron

Hi @Rakesh40 ,

 

var req = new GlideRecord('sc_request');
req.addEncodedQuery('active=true^sys_updated_on<javascript&colon;gs.beginningOfOneYearAgo()');
req.query();
while(req.next()){
	var ritm = new GlideRecord('sc_req_item');
	ritm.addEncodedQuery('request='+req.getUniqueValue()+'^stateNOT IN3,4,7');
	ritm.query();
	if(!ritm.next()){
		req.request_state='closed_complete';
		req.update();
	}
	else{
		while(ritm.next()){
			var task=new GlideRecord('sc_task');
			task.addEncodedQuery('request_item='+ritm.getUniqueValue()+'^stateNOT IN3,4,7');
			task.query();
			if(!task.next()){
				ritm.state='3';
				ritm.update();
			}
		}
	}
}

 

 

Above script will first check for the open sc_request records and check for the available child sc_req_item records. If no active records are found it will close the sc_request record. If there is any open sc_re_item record then it will check for the child sc_task and if do not find any active record then it will close the sc_req_item record. If active sc_task is found then it will ignore the records.

 

Note:- I have used the OOB values of the state choice, Please confirm in your instance the state choices before using the script. 

Kindly mark my answer as Correct and Helpful based on the Impact.

 

Regards,

Sumanth Meda

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);
        }
    }
}