Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Completing Metric Instance calculation when ticket is closed

kssnyder
Kilo Explorer

I am using a few OOB Metrics in reports to identify duration of how long a incident ticket was assigned to a person or group, but if the ticket is closed, the last metric is not updated so that calculation is never completed. The base Metric article (http://wiki.service-now.com/index.php?title=Metric_Definition_Support) states that a script can be used to complete a metric instance when a ticket is closed, but I have not been sucessful in actually building one.

A co-worker suggested adding the following into the script section, but this did NOT stop the metric when the incident was closed:

if (!current.active) {
answer = false;
}

Any suggestions as to what the script should be are most appreciated.

The fact that the last metric is not automatically completed when a ticket is closed was alluded to in Reporting on the date/time a task was assigned to a person, but since I'm actually looking for the script I made a new post.

6 REPLIES 6

Tyler Hoge - Gl
Tera Guru

Is there any update to this? I am having the same issue and I have tried just about everything.


Hey Tyler.Hoge



I am currently facing the same challenge.Did you have any luck in finding a solution for this through script?



Regards


Bhaskar Chaitanya P


mrswann
Kilo Guru

364 views still no answer



for or an incident is possibly by design if potential to reopen?




for a change request we cannot reopen, but it's closed since it was closed. The metric instance is superfluous?



i want same thing but curious if really necessary, there must be some resources involved in every closed change clock still ticking


Frank
Mega Expert

I wanted a way to calculate how long a demand stays in each state and I wanted it to terminate when it reached the last state. I spent quite a bit of time trying to get this to work correctly. I used a combination of filed valuation duration and script calculations. Never worked, the last state would always remain open or it wouldn't record the last state. I finally settled on doing everything in a script calculation. It's not in production yet, but from my preliminary testing, it seems to do what I intended. I terminate the metrics when the record goes from active to inactive. So when it reaches the final state, that last state will have an elapsed time of zero. it's all a script calculation, no field value durations.



/*


* Metrics for tracking the changing state of demands.


*/



// If this is a new record, then create a


// metric record.


if (current.sys_created_on == current.sys_updated_on) {


      createMetric();



// If the record is active, the previous


// metric needs to be closed and the next


// one needs to be created.


} else if (current.active) {


      closeMetric();


      createMetric();



// If the record is inactive, the previous


// metric needs to be closed and the final


// one needs to be created and then closed.


} else if(!current.active) {


      closeMetric();


      createMetric();


      closeMetric();


}



/*


* Create a metric.


*/


function createMetric() {


      var mi = new MetricInstance(definition, current);


      var gr = mi.getNewRecord();


      gr.field_value = true;


      gr.start = current.sys_updated_on;


      gr.calculation_complete = false;


      gr.insert();


}



/*


* Close a metric.


*/


function closeMetric() {


      var gr = new GlideRecord('metric_instance');


      gr.addQuery('id', current.sys_id);


      gr.addQuery('calculation_complete', false);


      gr.addQuery('definition.type', 'calculation');


      gr.query();



      if (gr.next()) {


              var definition = new GlideRecord('metric_definition');


              definition.get(gr.definition);


              var mi = new MetricInstance(definition, current);


              mi.endDuration();


      }


}