Want to close the bulk requests in which both RITM and Task are closed completed

John1243
Tera Contributor

Hi All, 

 

Need to close the Request's which are still in open(Approved) state in which all the associated RITM's and Task are closed completed. 

Need suggestion on running a script to do it for a bulk of requests. 

 

Thanks.

1 REPLY 1

Joe S1
Kilo Sage

Hello,

 

Something like this run from a Fix Script should work, needs to be tested and validated first.

closeOpenRequests();

function closeOpenRequests(){
	//encodedQuery to filter on all REQ's that are active, open and approved.
	var encQry = 'active=true^state=1^request_state=in_process';
	var req = new GlideRecord('sc_request');
	req.addEncodedQuery(encQry);
	req.query();

	//Loop through all REQ records that match the encoded query.
	while(req.next()){
		var ritmClosed = true; //check value
		//Loop through all related RITM's for the REQ and check the State value.
		var ritm = new GlideRecord('sc_req_item');
		ritm.addQuery('request', req.sys_id);
		ritm.query();

		//If any RITM is NOT Closed Complete set check value to 'false'.
		while(ritm.next()){
			if(ritm.state != 3){
				ritmClosed = false;
			}
		}

		//Loop through all related TASK's for the REQ and check State value.
		var sctaskClosed = true; //check value
		var sctask = new GlideRecord('sc_task');
		sctask.addQuery('request', req.sys_id);
		sctask.query();

		//If any TASK is NOT Closed Complete set check value to 'false'.
		while(sctask.next()){
			if(sctask.state != 3){
				sctaskClosed = false;
			}
		}

		//If all RITM's AND all TASK's are Closed Complete
		//update the original REQ record to closed complete.
		if (ritmClosed && sctaskClosed){
			req.state = 3;
			req.request_state = 'closed_complete';
			req.update();
		}
	}
}