The CreatorCon Call for Content is officially open! Get started here.

Calculate resolution time

Viri
Tera Contributor

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 

2 REPLIES 2

Sai Shravan
Mega Sage

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

Regards,
Shravan
Please mark this as helpful and correct answer, if this helps you

Maik Skoddow
Tera Patron
Tera Patron

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