BR caused an error while updating the RITM to closure

Ananth11
Kilo Contributor

In a order guide we have mapped some catalog item. On submit of the request it will create Parent and Child RITM.

We have created a Business Rule(Auto closure of Req) which will trigger on Requested Item (sc_req_item) table(after insert ,Update)

This BR is used to auto close the request only If the request contains a particular catalog item and no other RITM's in open state.

In Flow designer we have created a flow to fulfill the request. While Close complete the RITM from flow we are getting below error.
The error because of the Business Rule I have mentioned above.

Error Message:
Error occured while updating record: Operation against file 'sc_req_item' was aborted by Business Rule 'commentslogging^8d2188688702c1101df1b8cd8bbb3529'. Business Rule Stack:commentslogging,Auto closure of Req

I request your help to fix the issue.

Business Rule Script:

var getRITM = new GlideRecord('sc_req_item');
getRITM.addActiveQuery();
getRITM.addQuery('request',current.request);
getRITM.query();
if(getRITM.next())
{

//Don't close REQ as One of the RITM Is open.
gs.addInfoMessage('REQ cant be closed because one of the RITM is Open');
current.setAbortAction(true);
}

else
{

var req = new GlideRecord('sc_request');
req.addQuery('sys_id',current.request);
req.query();

if(req.next())
{
req.request_state = 'closed_complete';//close complete the sc request state
req.stage = 'closed_complete';
req.update();
}
}

 

6 REPLIES 6

Sai Kumar B
Mega Sage
Mega Sage

@Ananth Set your BR as Async so, it will not impact user session and modify the business rule as below

var getRITM = new GlideRecord('sc_req_item');
getRITM.addEncodedQuery('state=1^request='+current.request); //It will check if any RITM is open in current REQ
getRITM.query();
if(getRITM.hasNext()) //This goes inside if if any RITM is open in current REQ
{
//Don't close REQ as One of the RITM Is open.
gs.addInfoMessage('REQ cant be closed because one of the RITM is Open');
current.setAbortAction(true);
}

else
{

var req = new GlideRecord('sc_request');
req.addQuery('sys_id',current.request);
req.query();

if(req.next())
{
req.request_state = 'closed_complete';//close complete the sc request state
req.stage = 'closed_complete';
req.update();
}
}

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

update as this

Don't rely on Active field to check RITM is open; use state field

1) Make BR as Async

2) script as this

3) use setLimit(1) to get only 1 record

4) use hasNext() and not next() as you just want to verify record exists or not

var getRITM = new GlideRecord('sc_req_item');
getRITM.addEncodedQuery('stateIN-5,1,2'); // state is open, in progress or pending
getRITM.addQuery('request',current.request);
getRITM.setLimit(1);
getRITM.query();
if(getRITM.hasNext())
{
	//Don't close REQ as One of the RITM Is open.
	gs.addInfoMessage('REQ cant be closed because one of the RITM is Open');
	current.setAbortAction(true);
}
else
{
	var req = new GlideRecord('sc_request');
	req.addQuery('sys_id',current.request);
	req.query();
	if(req.next())
	{
		req.request_state = 'closed_complete';//close complete the sc request state
		req.stage = 'closed_complete';
		req.update();
	}
}

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

@Ananth 

Thank you for marking my response as helpful.

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Ananth11
Kilo Contributor

Thanks Ankur, I will try this code and let you know.