Metric Definition Script - Incidents Reoepened where reopen_count > 0

SB87
Tera Expert

I'm new to scripting, so please excuse my inexperience.

 

Objective: to create a metric instance when an incident is reopened where the reopen_count > 0 

Have tried: creating a field metric definition using the reopen_count field.

Issue: when a new incident gets created, an metric instant gets created where the reopen_count = 0 

 

Can someone please help create a script that records when an incident is reopened but ignores when the reopen_count = 0 ?

 

Many thanks!

 

 

1 ACCEPTED SOLUTION

SB87
Tera Expert

This script appears to provide what I was looking for. Post updated in the hope it may help others:

if (parseInt(current.getValue("reopen_count"),10) > 0) {
	var now = new GlideDateTime();
	var mi = new GlideRecord("metric_instance");
	mi.initialize();
	mi.setValue("definition", definition.getValue("sys_id"));
	mi.setValue("table", definition.getValue("table"));
	mi.setValue("field", definition.getValue("field"));
	mi.setValue("id", current.getValue("sys_id"));
	mi.setValue("start", now);
	mi.setValue("end", now);
	mi.setValue("calculation_complete", true);
	mi.setValue("value", current.getValue("reopen_count"));
	mi.setValue("field_value", current.getValue("reopen_count"));
	mi.insert();
}

 

Metric Instance data showing what is recorded:

sal87_0-1695645880530.png

  • Same Incident was cycled between "in-progress" and "resolved" for testing purposes
  • Each reopened event was recorded against the same incident
  • This includes time (start) and value (the number of times it has been reopened)
  • Can return the maximum value to find out how many times an incident has been reopened (in this case, 5 times).

Kind regards,
Sal

View solution in original post

7 REPLIES 7

Manoj89
Giga Sage

You can try scripting something like this 

 

// definiton: GlideRecord - this row
// current: current record
    if (current.reopen_count != 0)
        createMd();
    else
        closeMd();

//calls the script include "MetricInstance"
function createMd() {
    var md = new MetricInstance(definition, current);

    // 	check if a metric instance exists already
    if (md.metricExists()) {
        return;
    }

    var gr = md.startDuration();
}

function closeMd() {
    var md = new MetricInstance(definition, current);

    if (md.metricExists()) {
        md.endDuration();
    }
}

 

MetricInstance is a script include that comes OOB Script Include 

Hi,

The script almost works, however if I try reopening the same incident a second time it doesn't record this as a metric instance. It only displays records where reopen_count = 1.

Any suggestions?

Thanks!

Manoj89
Giga Sage

okay, try extending the code like below, I haven't tested it but 90% it should work

 

Note: in my instance 7 is the state closed where it is not allowed to reopen an incident.

 

 

 

 

// definiton: GlideRecord - this row
// current: current record
    if (current.state != 7 && current.reopen_count == 0)
        createMd();
    else if(current.state != 7 && current.reopen_count >= 1)
         updateMd();
    else
        closeMd();

//calls the script include "MetricInstance"
function createMd() {
    var md = new MetricInstance(definition, current);

    // 	check if a metric instance exists already
    if (md.metricExists()) {
        return;
    }

    var gr = md.startDuration();
}

function closeMd() {
    var md = new MetricInstance(definition, current);

    if (md.metricExists()) {
        md.endDuration();
    }
}

function updateMd() {
    var md = new MetricInstance(definition, current);

    if (md.metricExists()) {
        md.updateDuration();
    }else{
       md.startDuration();
    }
}

 

Add the following code to the script include

 

 

 

updateDuration: function() {
        var gr = new GlideRecord('metric_instance');
        gr.addQuery('definition', this.definition.sys_id);
        gr.addQuery('id', this.current.sys_id);
        gr.addQuery('calculation_complete', false);
        gr.query();
        if (gr.next()) {
            gr.value = this.current.reopen_count;
            gr.update();
        }
    },

 

 

 

Hi,

Thanks for your reply.

Sorry, I'm not understanding how I should 'include' the last part of your suggestion. When I've tried adding the last part, it returns a syntax error. Can you please try sending over the full script so that I can do a full replace?

NB. on my instance, state code 7 is also "closed" and cannot be reopened.

Thanks!