Metric Objects
When creating Metrics, there are a few methods that are used with little to no documentation (at least that I can find). This Script is in Metric Definitions to create and update Metric Instances
Script Include: https://instance.service-now.com/nav_to.do?uri=sys_script_include.do?sys_id=44c7c3a40a25810200e0dbdf70ea7f0c
Use
var mi = new MetricInstance(definition, current);mi.function();
Available Functions
Function Purpose process Drives field value duration, calls the start and end duration functions (which also creates the Metric Instance) startDuration Determines start duration for the record and calls getNewRecord which inserts the Metric Instance record endDuration Determines end duration for the record and updates the Metric Instance record getNewRecord Creates new Metric Instance record metricExists Determines if the current record already has an existing Metric Instance
Script Include
//Definition = Metric Definition record from which this script is being called //Current = Target Record 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(); }, 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() { } }