Incident autoclosure in 5 Business days

Marcin Witosla1
Tera Expert

Hello ,

Is there a working solution to autoclose incidents after 5 business days excluding weekends and holidays?

I have searched other community topics but any of them not working

The best solution is to created a scheduled job fired daily and checking incidents resolved at date.

Any ideas?

Regards

Marcin

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@Marcin Witoslawski 

You will have to check whether the current date is more than 5 business days of the updated time of the INC

// This script automatically closes incidents that are resolved
// and haven't been updated in the specified number of days.
// This number is a property in System Properties.
// To place a comment in the incident, uncomment the "gr.comments" line.

autoCloseIncidents();

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

	if (pn > 0) {
		var gr = new GlideRecord('incident');
		gr.addQuery('incident_state', '6');
		//gr.addQuery("number", "INC0000054");
		gr.addQuery('sys_updated_on', '<', queryTime);
		gr.query();
		while(gr.next()) {

			var resolvedTime = new GlideDateTime(gr.sys_updated_on);
			var days = 5;
			var nowDateTime = new GlideDateTime();
			var dur = new GlideDuration(60*60*24*1000*days);
			var schedule = new GlideSchedule('090eecae0a0a0b260077e1dfa71da828'); // give the sys_id of the schedule which is excluding weekends and holidays
			var finalTimeAfter5BusinessDays = schedule.add(resolvedTime, dur);

			// check if the 5 business days have crossed
			if(nowDateTime.getNumericValue() > finalTimeAfter5BusinessDays.getNumericValue()){
				gr.incident_state = '7';
				//  gr.comments = 'Incident automatically closed after ' + pn + ' days in the Resolved state.';
				gr.active = false;
				gr.update();
			}
		}
	}
}

another approach here

How to Auto-Close Resolved Cases (5 Business Days)

Regards
Ankur

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

View solution in original post

13 REPLIES 13

Anurag Tripathi
Mega Patron
Mega Patron

Hi,

Have you seen this ->Configure incidents to close automatically | ServiceNow Docs

This is on oob way to do the same, and it is driven by a scheduled job.

-Anurag

-Anurag

Thank you for reply. Yes i have checked that. It doesnt cover holidays . In Scheduled job schedule needs to be queried i think.

If you see the job you can add a schedule to it.

-Anurag

Ankur Bawiskar
Tera Patron
Tera Patron

@Marcin Witoslawski 

You will have to check whether the current date is more than 5 business days of the updated time of the INC

// This script automatically closes incidents that are resolved
// and haven't been updated in the specified number of days.
// This number is a property in System Properties.
// To place a comment in the incident, uncomment the "gr.comments" line.

autoCloseIncidents();

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

	if (pn > 0) {
		var gr = new GlideRecord('incident');
		gr.addQuery('incident_state', '6');
		//gr.addQuery("number", "INC0000054");
		gr.addQuery('sys_updated_on', '<', queryTime);
		gr.query();
		while(gr.next()) {

			var resolvedTime = new GlideDateTime(gr.sys_updated_on);
			var days = 5;
			var nowDateTime = new GlideDateTime();
			var dur = new GlideDuration(60*60*24*1000*days);
			var schedule = new GlideSchedule('090eecae0a0a0b260077e1dfa71da828'); // give the sys_id of the schedule which is excluding weekends and holidays
			var finalTimeAfter5BusinessDays = schedule.add(resolvedTime, dur);

			// check if the 5 business days have crossed
			if(nowDateTime.getNumericValue() > finalTimeAfter5BusinessDays.getNumericValue()){
				gr.incident_state = '7';
				//  gr.comments = 'Incident automatically closed after ' + pn + ' days in the Resolved state.';
				gr.active = false;
				gr.update();
			}
		}
	}
}

another approach here

How to Auto-Close Resolved Cases (5 Business Days)

Regards
Ankur

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