Auto-close RITMs not approved within 30 days

Tracey Wilmot
Tera Contributor

Currently, we are running a report on a daily basis to check for unapproved requests and manually marking them as Closed Incomplete if the request is not approved within 30 days.  We are looking for a way to automate this.

I think I need to setup a Scheduled Job but I'm not sure what the content of the script should be.  Any help would be greatly appreciated.

Many thanks
Tracey

1 ACCEPTED SOLUTION

Yes, you can trigger an email here. Refer to screenshot below from the above code which I have shared:

find_real_file.png

So just after gs.log line you can use below syntax to trigger your notification:

gs.eventQueue('Event Name',children,recipeint1, recipient2);

//Replace recipeint1 & recipeint2 should be the user object to whom you want to send the email to. 
Also Event Name need to be configured by navigating to "Registry" module and then use it here.

 

Then go to Notification module and use Trigger as Event is fired and select the same event name and make sure to check below parameter as True in Whom to receive tab:

Parm1

Parm2

Send to Event Creator

 

Then finally define the Notification content.

Regards,

Shloke

Hope this helps. Please mark the answer as correct/helpful based on impact.

Regards,
Shloke

View solution in original post

18 REPLIES 18

shloke04
Kilo Patron

Hi,

Please follow below steps to achieve your requirement:

1) Create a System Property by typing "sys_properties.LIST" in Application Navigator search menu as shown below:

find_real_file.png

2) Create a New Property with name "glide.ui.autoclose.time_req" and set the value as "30"  as shown below:

Make sure Type is selected as "Integer" as shown below:

find_real_file.png

3) Now create an "After" Business rule on Requested Item Table and use the script below:

find_real_file.png

Script:

autoCloseRITM();

function autoCloseRITM() {
	var ps = gs.getProperty('glide.ui.autoclose.time_req');
	var pn = parseInt(ps);
	var queryTime = new GlideDateTime();
	queryTime.addDaysUTC(-pn);

	if (pn > 0) {
		var gr = new GlideRecord('sc_req_item');
		gr.addEncodedQuery('approval=requested^sys_created_onONLast 30 days@javascript:gs.beginningOfLast30Days()@javascript:gs.endOfLast30Days()');
		gr.addQuery('sys_updated_on', '<', queryTime);
		gr.query();
		while(gr.next()) {
			gr.state = 3;
			gr.active = false;
			gr.update();
		}
	}
}

Now this BR will be executed using a Scheduled Job as shown below:

1) Navigate to "Today Scheduled Job" module as shown below:

find_real_file.png

2) Click on New

3) find_real_file.png

Hope this helps. Please mark the answer as correct/helpful based on impact.

Regards,
Shloke

Hope this helps. Please mark the answer as correct/helpful based on impact.

Regards,
Shloke

If this does not work you can directly use the below script in a Scheduled Job:

Navigate to Scheduled Job module under System Definition and then select Run a Script 

find_real_file.png

var gr = new GlideRecord("sysapproval_approver");


gr.addQuery('sysapproval.sys_class_name','sc_req_item'); //since all the approvals are generated only for RITMS


gr.addQuery('state','requested');


gr.addQuery('sys_created_on','<',gs.daysAgo(30));


gr.query();




while (gr.next()){


  var num = gr.sysapproval.number;


  gr.state='not_required';   // set approval state to No longer required


  cancelKids(num);                   // cancel the RITMS


  gr.setWorkflow(false);       // deactivate all the BR which are running


  gr.update();


}






function cancelKids(n) {


  var children = new GlideRecord('sc_req_item');


  children.addQuery('number',n);


  children.query();




  while (children.next())


  if (children.stage != 'Complete') {


  gs.log(" Request item Number "+ gr.sysapproval.number + " has been canceled ");


  var id = children.request;         // store the sys_id of REQ which is related to RITM


  children.stage = 'Request Cancelled';


  children.active = false;


  children.state = '4';                 // set RITM state to closed incomplete


  cancelParent(id);                         // this will cancel the REQ associated with the corresponding RITM


  children.setWorkflow(false);


  children.update();




  }


}




function cancelParent(id){


  var scReq = new GlideRecord("sc_request");


  scReq.addQuery('sys_id',id);


  scReq.query();


  if(scReq.next()){


  scReq.state = '4';


  scReq.stage = 'closed_incomplete';


  scReq.request_state = 'closed_cancelled';     // set REQ TO cancelled when all the RITMS are cancelled


  scReq.setWorkflow(false);


  scReq.update();


  }




}

 

Hope this helps. Please mark the answer as correct/helpful based on impact.

Regards,
Shloke

Hope this helps. Please mark the answer as correct/helpful based on impact.

Regards,
Shloke

Hello! 

Sorry to hijack an old thread, but I'm trying to use this method for my own purposes. 
Is there a way that I can run this but exclude a few types of RITM's? 

Thanks!

Thanks Shloke!

I'm going to test this and let you know how I get on.

Sure @Tracey Wilmot 

 

Let me know if you are stuck or having issue with the code I shared above. Do try both the methods in case if the first one is not working.

 

Regards,

Shloke

Hope this helps. Please mark the answer as correct/helpful based on impact.

Regards,
Shloke