The CreatorCon Call for Content is officially open! Get started here.

Closing a Request only when all the corresponding Requested Items are moved to closed.

prudhvig
Tera Expert

Hi,

I am working on the Request (Service Catalog) applciation in Servicenow.

Currently, in my instance:

1. Requested Items can be closed only when it's catalog tasks are moved to Complete.

2. In the same way, Requests are also being closed when catalog tasks are Complete, but do not wait for the attached Requested Items to get completed.

I want the Request to be completed only when the corresponding RITMs are closed. How can I achieve that? Does Servicenow provide any out-of-the-box BR or client script to handle this?

Please let me know. Thanks in advance.

3 REPLIES 3

Aakash Shah4
Tera Guru

Hi,



There is an OOB BR named "Close Parent if Required".


This Br will close the REQ automatically when all the RITMS are closed.


You can modify that according to your requirement


or


You can create a BR on ritm table as follows



closeREQ();



function closeREQ(){


  var req = new GlideRecord('sc_request');


  req.addQuery('active', true);


  req.query();


  while (req.next()) {



  var ritm = new GlideRecord('sc_req_item');


  ritm.addQuery('active', true);


ritm.addQuery('request',req.sys_id);


  ritm.query();


  if (ritm.hasNext()) {


//if not closed abort the action


  current.setAbortAction(true);


        gs.addErrorMessage("Please close the RITM before closing the request");


  }


  else {


  req.state = '3'; // closed


  req.update();


  }


  }



}


Aakash Shah4
Tera Guru

I have updated my BR


Check this


Run the BR on sc_request table as follows


find_real_file.png



Here is the script


find_real_file.png



(function executeRule(current, previous /*null when async*/) {



// Add your code here



var ritm = new GlideRecord('sc_req_item');


ritm.addQuery('active', true);


ritm.addQuery('request',current.sys_id);


ritm.query();


if (ritm.hasNext()) {


//if not closed abort the action


current.setAbortAction(true);


gs.addErrorMessage("Please close the RITM before closing the request");


}


else {


current.state = '3'; // closed


}



})(current, previous);


Talha3
Kilo Guru

@Aakash Shah4 
Your script automatically closed the RITM associated to the Req.
Was it working for you?