Calculate resolution time
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
â09-08-2023 01:46 AM
I would like to write a script for query all Incidents where the State is "Resolved" or "Closed" and
calculate the duration between opened_at and resolved_at value. Write the result
to the "Resolution time" field.
Please guide me for writing script
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
â09-08-2023 02:29 AM
Hi @Viri
Give a try with the below code :
var incidentGR = new GlideRecord('incident');
incidentGR.addQuery('state', 'IN', 'Resolved,Closed');
incidentGR.query();
while (incidentGR.next()) {
var openedAt = new GlideDateTime(incidentGR.opened_at);
var resolvedAt = new GlideDateTime(incidentGR.resolved_at);
var durationMillis = resolvedAt.getNumericValue() - openedAt.getNumericValue();
// Convert duration to days, hours, minutes, and seconds
var durationDays = durationMillis / (1000 * 60 * 60 * 24);
var durationHours = (durationMillis / (1000 * 60 * 60)) % 24;
var durationMinutes = (durationMillis / (1000 * 60)) % 60;
var durationSeconds = (durationMillis / 1000) % 60;
// Create a 'Resolution time' field in the incident record
incidentGR.resolution_time = durationDays + 'd ' + durationHours + 'h ' + durationMinutes + 'm ' + durationSeconds + 's';
incidentGR.update();
}
Please mark this as helpful and correct answer, if this helps you
Shravan
Please mark this as helpful and correct answer, if this helps you
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
â09-08-2023 02:33 AM
Hi @Viri
a simple duration calculation as already provided here might not be the best approach for you, as this topic is really complicated.
I recommend having a look into the description of the OOTB Script Include "DurationCalculator" (/nav_to.do?uri=sys_script_include.do?sys_id=c14b7dd30a6a803f2f25e0e60a457f7b ) to get an idea what everything you have to consider (time zones, business calendars, etc.). You will also find sample code for leveraging that Script Include which is also used throughout the platform.
Maik
Maik