Metric for calculation time spent by a group on the ticket

Umang Tandon
Kilo Contributor

Hi,

Need: To calculate and store the time for which a Ticket has been assigned to a particular group and even if it reassigned to the same group again.

Steps: Created a metric on the basis of assignment group but i the duration was coming zero seconds as the start time and end time is the same, but actually it should calculate the time for which the ticket has been assigned to the group.

Thanks for your help!

11 REPLIES 11

Umang Tandon
Kilo Contributor

I used this metric script

(function calculateMetric(current, definition, mi) {

 

      // Check to see if a metric instance already exists for this ticket

 

      // assigned to this assignment group

 

      var grMetric = new GlideRecord('metric_instance');

 

      grMetric.addQuery('id', current.getValue('sys_id'));

 

      grMetric.addQuery('definition', definition.getValue('sys_id'));

 

      grMetric.addQuery('value', current.getDisplayValue('assignment_group'));

 

      grMetric.query();

 

 

 

 

 

      if (grMetric.hasNext()) {

 

              // If so, then this ticket has already been counted for this assignment

 

              // group and there's no need to do anything.

 

      }

 

      else {

 

              // If not, create one

 

              var now = new GlideDateTime();

 

              var instant = new GlideDuration(0);

 

           

 

              grMetric = new GlideRecord('metric_instance');

 

              grMetric.initialize();

 

              grMetric.setValue('table', current.getRecordClassName());

 

              grMetric.setValue('id', current.getValue('sys_id'));

 

              grMetric.setValue('definition', definition.getValue('sys_id'));

 

              grMetric.setValue('field', definition.getValue('field'));

 

              grMetric.setValue('value', current.getDisplayValue('assignment_group'));

 

              grMetric.setValue('duration', instant);

 

              grMetric.setValue('business_duration', instant);

 

              grMetric.setValue('calculation_complete', true);

 

              grMetric.setValue('start', now);

 

              grMetric.setValue('end', now);

 

              grMetric.insert();

 

      }

 

})(current, definition, mi);

Umang Tandon
Kilo Contributor

I used this metric 

(function calculateMetric(current, definition, mi) { // Check to see if a metric instance already exists for this ticket // assigned to this assignment group var grMetric = new GlideRecord('metric_instance'); grMetric.addQuery('id', current.getValue('sys_id')); grMetric.addQuery('definition', definition.getValue('sys_id')); grMetric.addQuery('value', current.getDisplayValue('assignment_group')); grMetric.query(); if (grMetric.hasNext()) { // If so, then this ticket has already been counted for this assignment // group and there's no need to do anything. } else { // If not, create one var now = new GlideDateTime(); var instant = new GlideDuration(0); grMetric = new GlideRecord('metric_instance'); grMetric.initialize(); grMetric.setValue('table', current.getRecordClassName()); grMetric.setValue('id', current.getValue('sys_id')); grMetric.setValue('definition', definition.getValue('sys_id')); grMetric.setValue('field', definition.getValue('field')); grMetric.setValue('value', current.getDisplayValue('assignment_group')); grMetric.setValue('duration', instant); grMetric.setValue('business_duration', instant); grMetric.setValue('calculation_complete', true); grMetric.setValue('start', now); grMetric.setValue('end', now); grMetric.insert(); } })(current, definition, mi);

Umang Tandon
Kilo Contributor

All the predefined metric are storing the data sometimes not every time.... I am new to Service Now so not much aware of all these functionalities.

Yeah Metric Definitions & Instances are an old utility that has always needed a bit of a facelift.
I'll peek at your script later today.

Hi Robert,

Did you get time to check the script as I was not able to find any error in it. 
Any help will be very thankful.