Metric - script calculation - update after insert?

dave_shuman
Kilo Explorer

My intention is to create a metric when an incident is created, then update it when a field is populated.

Something like this:

if(!current.opened_at.nil())
createMetric();

if(!current.u_first_contact.nil())
updateMetric();

I've been able to get each function to write to the log, what I have failed at is the proper manipulation of the Metric object. There doesn't seem to be many details on the wiki as to what methods we have on that object. I'd like to create the Metric, then at a later change in the record, Update the metric.

This code doesn't work, but it shows some of my intentions:

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

var gr = mi.getNewRecord();
gr.start = current.sys_created_on;
gr.calculation_complete = false;
gr.insert();
}

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

var gr = mi;
gr.end = current.u_first_contact;
gr.duration = gs.dateDiff(gr.start.getDisplayValue(), gr.end.getDisplayValue());
gr.calculation_complete = true;
gr.update();
}

Does anyone have an advanced Metric they would be willing to share? Or any thoughts as to what I might try differently?

Particularly around what can be done with MetricInstance.

7 REPLIES 7

hartr
Giga Contributor

Did you get anywhere with this ? I'm looking to do something similar.


GregJWillis
ServiceNow Employee
ServiceNow Employee

I am also trying to update an existing Metric record. Any progress?


Did you guys ever get this working? I need a similar result.


Hi, what i ended up doing to measure this was use the Metric Definition named "Incident State Duration". Basically this records time spent within each incident on each state. The hardest bit about this was actually getting our technicians to use the incident state field and put it to work in progress! Once they got into the habit its fine now, its not an exact science as we are only human and sometimes people can and do forget but it does give us a better feel for things (some have also used it to cheat a little!). The table i used to then report on this is Incident Metric with a condition of Value=Work In Progress and put a list calculation on the duration field to get a total. Hope this helps and sorry for the late response. Thanks Dawn