Help With Incident Metric calculation.

Scott29
Kilo Sage

Hello, I have an incident metric (script for the metric below). The metric calculates the duration of the time it took for the ticket, from creation to the ticket being assigned to a group. The metric has an unused "Value" field, as pictured in the screenshot. How can I update the script to convert the duration of minutes, hours, and days to an XX.xx numeric value? Converted into just hours. Then, populate that number in the value.

 

Scott29_0-1722390973038.png

 

 

 

 

 

 

// variables available
// current: GlideRecord -  target incident
// definition: GlideRecord -  (this row)
var g = current.assignment_group;
if (g !="")
  createMetric();

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

  var gr = mi.getNewRecord();
  gr.start = current.sys_created_on;
  gr.end = current.u_time_to_assignment_group;
  gr.duration = gs.dateDiff(gr.start.getDisplayValue(), gr.end.getDisplayValue());
  gr.calculation_complete = true;
  gr.insert();
}
 
 
 
1 ACCEPTED SOLUTION

SanjivMeher
Kilo Patron
Kilo Patron

You can try something like this

 

var dur_seconds = gs.dateDiff(gr.start.getDisplayValue(), gr.end.getDisplayValue(),true);
gr.value = Math.round(dur_seconds / 3600)+' Hrs';

 


Please mark this response as correct or helpful if it assisted you with your question.

View solution in original post

2 REPLIES 2

SanjivMeher
Kilo Patron
Kilo Patron

You can try something like this

 

var dur_seconds = gs.dateDiff(gr.start.getDisplayValue(), gr.end.getDisplayValue(),true);
gr.value = Math.round(dur_seconds / 3600)+' Hrs';

 


Please mark this response as correct or helpful if it assisted you with your question.

Satishkumar B
Giga Sage
Giga Sage

Hi @Scott29 
try this:

 

 

// Variables available
// current: GlideRecord - target incident
// definition: GlideRecord - (this row)
var g = current.assignment_group;
if (g != "")
  createMetric();

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

  var gr = mi.getNewRecord();
  gr.start = current.sys_created_on;
  gr.end = current.u_time_to_assignment_group;
  
  // Calculate duration in milliseconds
  var durationMillis = gs.dateDiff(gr.start.getDisplayValue(), gr.end.getDisplayValue(), true);
  
  // Convert duration to hours and round to two decimal places
  var durationHours = Math.round((durationMillis / (1000 * 60 * 60)) * 100) / 100;
  
  gr.duration = gs.dateDiff(gr.start.getDisplayValue(), gr.end.getDisplayValue());
  gr.value = durationHours; // Store duration in hours as a numeric value
  gr.calculation_complete = true;
  gr.insert();
}

 

 

 

…………………………………………........................................................................................
Mark it helpful 👍and Accept Solution !! If this helps you to understand.

…………………………………………........................................................................................