Auto close incident after 3 business days
Closes Incidents in a “Resolved” state if they haven’t been updated within a specified number of hours. The duration is calculated based on a predefined schedule (e.g., business hours).
You can use below script in business rule/ Script include/ Fix script to close the incident. You can close problem/Request with same code just you need to adjust the table, fields and value according to target table.
You can use below script in business rule/ Script include/ Fix script to close the incident. You can close problem/Request with same code just you need to adjust the table, fields and value according to target table.
// Get the auto-close time threshold from system properties var autoCloseHours = parseInt(gs.getProperty('glide.ui.autoclose.time', '0')); // Default to 0 if property is undefined if (autoCloseHours > 0) { // Retrieve the schedule by name var scheduleRecord = new GlideRecord('cmn_schedule'); if (!scheduleRecord.get('name', '8-5 weekdays excluding holidays')) { gs.error("Schedule '8-5 weekdays excluding holidays' not found."); return; } // Initialize the schedule var schedule = (typeof GlideSchedule !== 'undefined') ? new GlideSchedule(scheduleRecord.sys_id) : new Packages.com.glide.schedules.Schedule(scheduleRecord.sys_id); // Get the current date/time for calculations var now = new GlideDateTime(); // Query for incidents in the Resolved state var incidentGR = new GlideRecord('incident'); incidentGR.addQuery('state', 6); // 6 = Resolved state incidentGR.query(); while (incidentGR.next()) { // Calculate the duration since the last update, based on the schedule var lastUpdated = incidentGR.sys_updated_on.getGlideObject(); var duration = schedule.duration(lastUpdated, now); // Convert duration to hours var totalHours = duration.getDayPart() * 24 + parseInt(duration.getDurationValue().split(':')[0]); // Close incidents that exceed the auto-close threshold if (totalHours >= autoCloseHours) { incidentGR.state = 7; // 7 = Closed state incidentGR.active = false; // incidentGR.comments = 'Incident automatically closed after ' + autoCloseHours + ' hours in the Resolved state.'; incidentGR.update(); } } }
Step by step explanation
1. Get Threshold
var ps = gs.getProperty('glide.ui.autoclose.time'); // Fetch the threshold (in hours) from a system property var pn = parseInt(ps); // Convert the value to an integer
Create property glide.ui.autoclose.time
in table “sys_properties” and assign the number of hours after which you want to close the incident after resolution.
2. Load a Schedule
var schedRec = new GlideRecord('cmn_schedule'); schedRec.get('name', '8-5 weekdays excluding holidays'); var sched = new GlideSchedule(schedRec.sys_id);
1. Create new schedule named “8-5 weekdays excluding holidays”. Navigate to System Scheduler > Schedules.
2. Used “8-5 weekdays excluding holidays” schedule to calculate business time.
3. Get the Current Date and Time
var actualDateTime = new GlideDateTime(); actualDateTime.setDisplayValue(gs.nowDateTime());
4. Query for Resolved Incidents
var gr = new GlideRecord('incident'); gr.addQuery('state', 6); // 'Resolved' state is represented by 6 gr.query();
5. Calculate the Time Difference
var difDay = sched.duration(gr.sys_updated_on.getGlideObject(), actualDateTime).getDayPart() * 24; var difHour = sched.duration(gr.sys_updated_on.getGlideObject(), actualDateTime).getDurationValue().split(':')[0].substr(-2); var dif = difDay + parseInt(difHour.replace(/^[0]+/g,""));
- The
sched.duration()
method calculates the duration between two dates considering the schedule. - The result is split into:
- Days: Converted to hours and added to the final difference.
- Hours: Parsed and added to the day-based hours for total time.
6. Close the Incident if Time Exceeds Threshold
if (dif >= pn) { gr.state = 7; // 'Closed' state is represented by 7 gr.active = false; // Mark the incident as inactive // gr.comments = 'Incident automatically closed after ' + pn + ' hours in the Resolved state.'; gr.update(); // Save the record }
If the calculated time (dif
) is greater than or equal to the threshold (pn
):
-
- The state is set to “Closed” (
7
). - The active flag is set to
false
. - Optionally, a comment can be added explaining the reason for closure.
- The state is set to “Closed” (
Key Considerations
-
- Schedule:
- Ensure the schedule exists in System Scheduler > Schedules.
- If you want the script to run 24/7, skip the schedule setup and calculate based on elapsed time only.
- System Property:
- Define
glide.ui.autoclose.time
in System Properties under System Definition > Properties. - Example value:
48
for 48 hours.
- Define
- States:
- State
6
: Resolved. - State
7
: Closed.
- State
- Script Placement:
- Deploy the script as a Scheduled Job under System Definition > Scheduled Jobs to run it periodically.
- Testing:
- Run the script in a test instance first to ensure no unintended closures occur.
- Schedule: