Blog

Find Active Requests with all Closed Requested Items

How to find any active requests that have all their requested items closed.  This can occur due to not setting the Stage field in your Item workflows.

In ServiceNow there is an important Business rule called "Close Parent if Required".  After each Requested Item is closed, it checks if all the related Requested Items are closed.  If all of them are closed, the Request is closed.

If you don't use Stages properly in your workflow, such as not using them at all, this can cause the Request to not close.

You can fix this by adjusting your workflows to have stages.  However to find all the existing Requests with this issue, you can run this script:

Find Active Requests with all Closed Requested Items

1. Elevate your privileges to security admin
2. In scripts - background run this script

(function() {
	var requests = [];
	var grRequest = new GlideRecord("sc_request");
	grRequest.addActiveQuery();
	grRequest.query();
	while (grRequest.next()) {
		var gaRITM = new GlideAggregate("sc_req_item");
		gaRITM.addQuery("request",grRequest.getValue("sys_id"));
		gaRITM.addActiveQuery();
		gaRITM.addAggregate('COUNT');
		gaRITM.query();
		if (gaRITM.next()) {
			if (gaRITM.getAggregate('COUNT') == 0) {
				requests.push(grRequest.getValue("number"));
			}
		}
	}
	gs.print(requests);
})();

You can then use the results of this script to go out and close some requests.