Set metric instance end depending on other metric definition

flawed_81
Tera Guru

Hi all,

I have a requirement to track when a customer was not updated through additional comments on their incident. I know this can be done by creating a new field, however this is not an option for us.

I have created a database view to capture incidents and metric instances, and I have created two metric definitions, one to track when an additional comment is added to an incident (Incident Commented), one tracks when the incident was created (New Incident - to track incidents without comments). 

I need the New Incident instance to end when a new Incident Commented metric instance is created (so basically one will end the other).

How can I achieve this please?

 

So far I have written this code in the new Incident definition but it's not working to set the end field:

 

if (current.incident_state == 1) { // New
createMetric();
}

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

var gnr = mi.getNewRecord();
gnr.start = current.sys_created_on;
gnr.insert();
}

function closeMetricDuration() {
var gr = new GlideRecord('metric_instance');
gr.addQuery('id', current.sys_id);
gr.addQuery('calculation_complete', false);
gr.addQuery('definition_name', 'Incident Commented');
gr.query();
while (gr.next()) {
gnr.end = gr.start;
gnr.duration = gs.dateDiff(gr.start.getDisplayValue(), gr.end.getDisplayValue());
gnr.calculation_complete = true;
gnr.endDuration();
}
if (gr.metricExists()) {
closeMetricDuration(current);
}

}

I've found this post which had a similar requirements but I cannot get the code to work in mine and the new Incident is not creating a new instance in New Incident.

https://www.servicenow.com/community/developer-forum/how-to-stop-metric-timing-on-resolved-incident/...

 

1 ACCEPTED SOLUTION

flawed_81
Tera Guru

I solved this myself. For whoever is looking at a similar use case, I've left the "New Incident" script empty, and added this code to the "Incident commented" metric definition:

 

if (current.state != 7){ // not closed
answer = true;
closeMetricDuration(current);
}
 
function closeMetricDuration(current) {
    var gr = new GlideRecord('metric_instance');
    gr.addQuery('id', current.sys_id);
gr.addQuery('calculation_complete', false);
    gr.addQuery('definition.sys_id', 'f96d2bee1b684a901533a8a4bd4bcb54');
    gr.query();
    while (gr.next()) {
        var definition = new GlideRecord('metric_definition');
        definition.get(gr.definition);
        var mi = new MetricInstance(definition, current);
        mi.endDuration();
    }
}

 

 

View solution in original post

1 REPLY 1

flawed_81
Tera Guru

I solved this myself. For whoever is looking at a similar use case, I've left the "New Incident" script empty, and added this code to the "Incident commented" metric definition:

 

if (current.state != 7){ // not closed
answer = true;
closeMetricDuration(current);
}
 
function closeMetricDuration(current) {
    var gr = new GlideRecord('metric_instance');
    gr.addQuery('id', current.sys_id);
gr.addQuery('calculation_complete', false);
    gr.addQuery('definition.sys_id', 'f96d2bee1b684a901533a8a4bd4bcb54');
    gr.query();
    while (gr.next()) {
        var definition = new GlideRecord('metric_definition');
        definition.get(gr.definition);
        var mi = new MetricInstance(definition, current);
        mi.endDuration();
    }
}