Background script to close Requests when RITM is Closed complete

AdrianHolm
Tera Guru

Hello Community,

As the title says; I could need some help writing a background script to close all our Requests when RITM have State Closed complete. We have approximately 130 Requests where RITM is Closed complete, so I believe there is a issue with a BR in our instance. I'm currently on the path of learning to write my own scripts, but unfortunately this is out of my skills. 

Thanks in advance!

 

Best regards,
Adrian H

14 REPLIES 14

The above script will close all requests that are active and has no active ritm.

setWorkflow(false) -> This will ensure that when the request is being closed by the script, no other business rules run for that transaction.

 

What do you want to exclude from the logic, what requests should not be closed?

 

-Anurag

-Anurag

Some of our requests still have active RITMs with state Limited acceswhich means it will be Closed complete on a specific date. Requests like these has to remain active, so it's only requests where RITMs are Closed complete (3) we want to close. 

Ok,

 

Try this then

 

var req = new GlideRecord('sc_request');
req.addActiveQuery();
req.query();
while(req.next()){
// check if there are any active requested items under it
var req_item = new GlideRecord('sc_req_item');
req_item.addQuery('request',req.sys_id);
req_item.addQuery('state', 3);
req_item.setLimit(1);
if(!req_item.hasNext()){
req.state = '3';
req.active = false;
req.setWorkflow(false);
req.update();
}

}

-Anurag

It seems it still took the Active ones. Any other suggestions? 

Lets try the reverse

var req = new GlideRecord('sc_request');
req.addActiveQuery();
req.query();
while(req.next()){
// check if there are any active requested items under it
var req_item = new GlideRecord('sc_req_item');
req_item.addQuery('request',req.sys_id);
req_item.addQuery('state','!=', 3);
req_item.setLimit(1);
if(!req_item.next()){
req.state = '3';
req.active = false;
req.setWorkflow(false);
req.update();
}

}

 

Now, if there are any ritm under the req which is not closed complete, then no action, else it will close the req

-Anurag