My REQ is going to Closed Complete when RITM is open

Simran9
Tera Contributor

I wrote a business rule to Close Complete my REQ when all the RITMs in my REQ are closed complete. It works perfectly fine when I have more than one RITM in one REQ. But when I only add one RITM to one REQ, the REQ is already in Closed Complete state while the RITM is still in active state. Can anyone tell me what's wrong with my code?

(function executeRule(current, previous /*null when async*/) {
	//first see if there are any active RITMs, excluding the current record, with this same REQ
	var ritm = new GlideRecord('sc_req_item');
	ritm.addQuery('request', current.request);
	ritm.addQuery('sys_id', '!=', current.sys_id);
	ritm.addActiveQuery();
	ritm.query();
	//if there are not, and the REQ is still open, close it
	if (!ritm.next()) {
		var gr = new GlideRecord('sc_request');
		gr.addQuery('sys_id', current.request);
		gr.addActiveQuery();
		gr.query();
		if (gr.next()) {
			gr.request_state = 'closed_complete';
			gr.update();
		}
	}
})(current, previous);

.

 

1 REPLY 1

Anurag Tripathi
Mega Patron
Mega Patron

I guess this is happening because you are skipping the current RITM

Try this

(function executeRule(current, previous /*null when async*/) {
	//first see if there are any active RITMs, excluding the current record, with this same REQ
	var ritm = new GlideRecord('sc_req_item');
	ritm.addQuery('request', current.request);
	//ritm.addQuery('sys_id', '!=', current.sys_id);
	ritm.addActiveQuery();
	ritm.query();
	//if there are not, and the REQ is still open, close it
	if (!ritm.next()) {
		var gr = new GlideRecord('sc_request');
		gr.addQuery('sys_id', current.request);
		gr.addActiveQuery();
		gr.query();
		if (gr.next()) {
			gr.request_state = 'closed_complete';
			gr.update();
		}
	}
})(current, previous);
-Anurag