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

Script include --> https://xxx.service-now.com/sys_script_include.do?sys_id=44c7c3a40a25810200e0dbdf70ea7f0c

 

replace "xxx" with your instance domain

 

Complete Script Include

 

 

 

gs.include("PrototypeServer");

var MetricInstance = Class.create();

MetricInstance.prototype = {
    initialize: function(definitionGR, currentGR) {
        this.definition = definitionGR;
        this.current = currentGR;
    },

    // process is the driver for field value duration type definitions 
    process: function() {
        answer = true; 
        mi = this; // global variable
        eval(this.definition.script);
        if (!answer) 
           return; 

        this.endDuration();  // end any previous duration for this metric
        this.startDuration(); // start a new one    
    },

    startDuration: function() {
        var gr = this.getNewRecord();
        gr.field_value = this.current[this.definition.field];
        gr.start = current.sys_updated_on;
        gr.insert();
    },

    endDuration: 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())
          return;
          
        gr.end = this.current.sys_updated_on;
        gr.duration = gs.dateDiff(gr.start.getDisplayValue(), gr.end.getDisplayValue());
        gr.calculation_complete = true;
        gr.update();
    },

	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();
        }
    },

    getNewRecord: function() {
        var gr = new GlideRecord('metric_instance');
        gr.table = this.current.getRecordClassName();
        gr.id = this.current.sys_id;
        gr.definition = this.definition.sys_id;
        gr.field = this.definition.field;
        return gr;
    },

    // return true if a metric exists for this definition and current
    metricExists: function() {
        var gr = new GlideRecord('metric_instance');
        gr.addQuery("id", this.current.sys_id);
        gr.addQuery("definition", this.definition.sys_id);
        gr.query();
        return gr.hasNext();
    },

    _z : function() {
    }    
}

 

Thank you for your input, but I feel this proposed solution was overcomplicating what was needed. I have added a solution to this post that you may find helpful.

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