Metrics

Metric from Assignment Group to Close

This will require two metric definitions. One will track the assignment group field and the other will track the state field. When the assignment group is filled the metric begins and when the state turns to close, the metric will find the assignment group metric and end the duration.

Metric Definition

Name : Assignment Group has been populated
Type : Script calculation
if (!current.assignment_group.nil()) {
createMetric();
  }

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

      var gr = mi.getNewRecord();
      gr.field_value = current.assignment_group;
    gr.start = current.sys_updated_on;
      gr.calculation_complete = false;
      gr.insert();
  }

Metric Definition

Name : Stop Assignment duration on close
Type : Script calculation
  // Close Durations on Closed Complete, Closed Incomplete, or Cancelled
if (current.state == 3 ||  current.state == 4 || current.state == 7) {
    closeDurations(current);
}

function closeDurations(current) {
    var gr = new GlideRecord('metric_instance');
    gr.addQuery('id', current.sys_id);
    gr.addQuery('calculation_complete', false);
    gr.addQuery('definition.name', 'Time to closure');
    gr.query();
    while (gr.next()) {
       var definition = new GlideRecord('metric_definition');
       definition.get(gr.definition);
       var mi = new MetricInstance(definition, current);
       mi.endDuration();
    }
}

Warning

Both Metrics need to be placed as “Script calculation” or it will not process correctly