Business rule to autoclose resolved incidents after a certain number of business days not working
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-22-2015 02:20 PM
Hi,
My organization wants to create a new business rule to replace incident autoclose that will use business days instead of calendar days to determine when to close resolved incidents. To do this, I have created a business rule which references a schedule with a schedule entry that is all day and repeats every weekday. However, when I execute it either by putting it inside of a fix script and running it or telling a scheduled job to fire it, the incidents that were resolved more than 5 days ago don't seem to be closing. Here is the code:
// 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() {
gs.log('updated incident autoclose running');
var i = 0;
var ps = gs.getProperty('glide.ui.autoclose.time');
var pn = parseInt(ps);
if (pn > 0) {
var j = 0;
var countdays = pn;
var sid ='4a62d78a132e42001a1f58222244b074';
var sched = new GlideSchedule(sid);
gs.info(sched.getName());
while(countdays!=0 || !sched.isInSchedule(d)){
var d = new GlideDateTime();
d.setDisplayValue(gs.daysAgoStart(countdays).toString());
if(!sched.isInSchedule(d)){
gs.log('not in Schedule');
pn++;
j++;
}
countdays--;
}
gs.log('Number of days: ' + pn);
gs.log(gs.daysAgoStart(pn));
var gr = new GlideRecord('incident');
gr.addQuery('incident_state', '6');
gr.addQuery('sys_updated_on', '<', gs.daysAgoStart(pn));
gr.query();
while(gr.next()) {
gr.incident_state = '7';
i++;
// gr.comments = 'Incident automatically closed after ' + pn + ' days in the Resolved state.';
gr.active = false;
gr.update();
}
gs.log('Number of incidents affected:'+ i);
}
}
Does anyone have any idea what might be going on?
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-22-2015 02:55 PM
Please refer to this script instead:
// 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() {
gs.log('updated incident autoclose running');
var i = 0;
var ps = gs.getProperty('glide.ui.autoclose.time');
var pn = parseInt(ps);
if (pn > 0) {
var j = 0;
var countdays = pn;
var sched = new GlideSchedule('4a62d78a132e42001a1f58222244b074');
gs.info(sched.getName());
var d = new GlideDateTime();
var test = false;
var daycount = 1;
var countday = 1;
d.setDisplayValue(gs.daysAgoStart(countday).toString());
do {
if(sched.isInSchedule(d)){
gs.log(d.toString() + ' is in Schedule');
countday++;
daycount++;
} else {
gs.log(d.toString() + ' is not in Schedule');
countday++;
}
d.setDisplayValue(gs.daysAgoStart(countday).toString());
} while(!sched.isInSchedule(d) || daycount != pn)
gs.log('Number of days: ' + countday);
gs.log(gs.daysAgoStart(countday));
var gr = new GlideRecord('incident');
gr.addQuery('incident_state', '6');
gr.addQuery('resolved_at', '<', d);
gr.query();
while(gr.next()) {
// i++;
gs.log(gr.number);
gr.incident_state = '7';
// gr.comments = 'Incident automatically closed after ' + pn + ' days in the Resolved state.';
gr.active = false;
gr.update();
}
gs.log('Number of incidents affected:'+ i);
}
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-29-2016 04:36 PM
Robert, thank you for your script!
The one thing that tripped me up was on the schedule. I created a new schedule with a schedule entry of weekly on weekdays, starting today. The script below looks in the past, and I had to update my schedule to begin in the past (I chose two years in the past).
I've made a couple of tweaks and it's working very well for me.
- Added a ';' at the end of line 42 below
- Added a safety valve in case there's an issue with the schedule and never finds any business days.
- Various comments
Updated 'incident autoclose' business rule:
// 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() {
gs.log('updated incident autoclose running);
var i = 0;
var ps = gs.getProperty('glide.ui.autoclose.time');
var pn = parseInt(ps);
if (pn > 0) {
var j = 0;
var countdays = pn;
var sched = new GlideSchedule('1beb74144fa6d6008ffcb3728110c7d6'); //
gs.info(sched.getName());
var d = new GlideDateTime();
var test = false;
var daycount = 1;
var countday = 1;
var safetyint = 1;
d.setDisplayValue(gs.daysAgoStart(countday).toString());
do {
if(sched.isInSchedule(d)){
gs.log(d.toString() + ' is in Schedule');
countday++;
daycount++;
} else {
gs.log(d.toString() + ' is not in Schedule');
countday++;
}
gs.sleep(500); //slowed the script so the log file will be in order
safetyint++;
if (safetyint >= 50) {
gs.log('Stopping script due to hitting ' + safetyint + ' iterations.');
return; //ends the function
}
d.setDisplayValue(gs.daysAgoStart(countday).toString());
}while(!sched.isInSchedule(d) || daycount != pn);
gs.log('Number of days: ' + countday);
gs.log(gs.daysAgoStart(countday));
var gr = new GlideRecord('incident');
gr.addQuery('incident_state', '6');
gr.addQuery('resolved_at', '<', d);
gr.query();
while(gr.next()) {
i++;
gs.log(gr.number);
gr.incident_state = '7';
gr.comments = 'Incident automatically closed after ' + pn + ' days in the Resolved state.';
gr.active = false;
gr.update();
}
gs.log('Number of incidents affected:'+ i);
}
}
Hope this helps someone else.
-Jarod
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-01-2017 07:47 AM
do we need to create scheduled job for it, if yes can you give the steps to create the scheduled job

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-01-2017 08:13 AM
No need, as there's already an OOB scheduled job that calls an OOB BR. I typically just update the OOB BR with the above code.
System Scheduler > Scheduled Job: 'Autoclose Incidents'
System Definition > Business Rules: 'incident autoclose'
JarodM