Incident Metric - Calculate time between first assigned to until resolution.

Manan4
Kilo Contributor

One of our clients needs to have a report created that would show them the time taken to complete an INC after it was assigned to an ITIL User. Their SLA Definitions for resolution calculate the entire lifecycle of the INC from the time it was opened until closed. I came across the Metrics table and found the below Metric for INC which calculate the duration from Open to Resolved/Closed. 

Hoping to build a similar metric that would allow me to calculate duration from first assigned to until Resolved/Closed. Any help or suggestions would be much appreciated. Thank you.

find_real_file.png

1 ACCEPTED SOLUTION

Hajar BENJAHHAR
Mega Sage

Hello @Manan ,

So what I suggest to you is to create a field type Date/Time to store type when the field changes for the first time : 

find_real_file.png

To get the time when assigned to changes for the first time you have to create a before business rule as shown bellow : 

(function executeRule(current, previous /*null when async*/ ) {
	
    if (gs.nil(previous.assigned_to) && current.assigned_to) {
        var date_assigned_to = new GlideDateTime(gs.nowDateTime());
        current.u_assigned_to_changes_on = date_assigned_to;
    }

})(current, previous);

Results after assign incident to a user : 

find_real_file.png

 

The last step is to create a metric and start calculating : 

// current: GlideRecord -  target incident
/*if (current.assigned_to) {
        createMetricAssignedTo();
}*/
var state = current.state;
if(state>=6){
	createMetric();
}

function createMetric() {
  var mi = new MetricInstance(definition, current);
  if (mi.metricExists()) 
    return; 

  var gr = mi.getNewRecord();
  gr.start = current.u_assigned_to_changes_on;
  gr.end = current.sys_updated_on;
  gr.duration = gs.dateDiff(gr.start.getDisplayValue(), gr.end.getDisplayValue());
  gr.calculation_complete = true;
  gr.insert();
}

 Results : 

Closed incident :

find_real_file.png

 

 

find_real_file.png

 I hope that what you are looking for. 

Best regards.

View solution in original post

7 REPLIES 7

Rozmin Parpia
ServiceNow Employee
ServiceNow Employee

Instead of script, try using Field Duration.

I am also interested more on using the GUI rather than scripts.

Do you have any idea, how this can be done?

Hajar BENJAHHAR
Mega Sage

Hello @Manan ,

So what I suggest to you is to create a field type Date/Time to store type when the field changes for the first time : 

find_real_file.png

To get the time when assigned to changes for the first time you have to create a before business rule as shown bellow : 

(function executeRule(current, previous /*null when async*/ ) {
	
    if (gs.nil(previous.assigned_to) && current.assigned_to) {
        var date_assigned_to = new GlideDateTime(gs.nowDateTime());
        current.u_assigned_to_changes_on = date_assigned_to;
    }

})(current, previous);

Results after assign incident to a user : 

find_real_file.png

 

The last step is to create a metric and start calculating : 

// current: GlideRecord -  target incident
/*if (current.assigned_to) {
        createMetricAssignedTo();
}*/
var state = current.state;
if(state>=6){
	createMetric();
}

function createMetric() {
  var mi = new MetricInstance(definition, current);
  if (mi.metricExists()) 
    return; 

  var gr = mi.getNewRecord();
  gr.start = current.u_assigned_to_changes_on;
  gr.end = current.sys_updated_on;
  gr.duration = gs.dateDiff(gr.start.getDisplayValue(), gr.end.getDisplayValue());
  gr.calculation_complete = true;
  gr.insert();
}

 Results : 

Closed incident :

find_real_file.png

 

 

find_real_file.png

 I hope that what you are looking for. 

Best regards.

I used gr.end = current.closed_at; instead of the updated_on field. 

 

Works !

 

Thank you