Background script to close REQ and RITM when SCTASK is closed

Heather White
Mega Guru

We have our workflows set up to close the REQ and RITM when the SCTASK is closed.  I have discovered that a few workflows were not set up correctly, so there are some REQs and RITMs still open even though the SCTASK is closed.

I would like to set up a background script to close these records.  I have not written one of these before so I am not sure how to begin.

My logic is this:

Find all REQ and RITMs where the corresponding SCTASK is closed incomplete, closed complete or closed skipped.I have the values for these. 

Once these are identified, set all REQs and RITMs to closed complete.

Any help is greatly appreciated.

Thank you!

Heather

3 REPLIES 3

Alok Das
Tera Guru

HI Heather,

Please find the below script which you can use:

var req = new GlideRecord('sc_request');
req.addEncodedQuery('request_stateNOT INclosed_complete,closed_incomplete,closed_cancelled,closed_rejected,closed_skipped');
req.query();
while(req.next()){
	var ritm = new GlideRecord('sc_req_item');
	ritm.addEncodedQuery('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('stateNOT IN3,4,7');
			task.query();
			if(!task.next()){
				ritm.state='3';
				ritm.update();
			}
		}
	}
}

Note:- I have not tested this code, request you to please test it in sub prod instance once before you use in your production instance.

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

Regards,

Alok

You also might want to print the row count for each query to make sure it matches what you see in UI.

Please ignore the above script and find the updated one below:

var req = new GlideRecord('sc_request');
req.addEncodedQuery('request_stateNOT INclosed_complete,closed_incomplete,closed_cancelled,closed_rejected,closed_skipped');
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,

Alok