How-to Setup Metric Definition for Catalog Task

Austin20
Tera Contributor

Hey Community. I'm trying to set up a metric definition to calculate create to resolve duration. I copied the metric definition for incident and made a modification to it. I created a test ticket and I ended up getting time data in the duration field. When I try to report off that I get zero seconds. I see data is in the metric definition that I created. From looking at the incident metric definition it only has one row. Where I have two rows in my new metric definition. Below is the code that I'm using for the sc_tasks. Also attached a picture on what I see in the metric definition.  

 

 

// variables available
// current: GlideRecord -  target sc_tasks
// definition: GlideRecord -  (this row)
var s = current.state;
if (s >= 6)
  createMetric();

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

  var gr = mi.getNewRecord();
  gr.start = current.sys_created_on;
  gr.end = current.sys_updated_on;
  gr.duration = gs.dateDiff(gr.start.getDisplayValue(), gr.end.getDisplayValue());
  gr.calculation_complete = true;
  gr.insert();
}
1 ACCEPTED SOLUTION

Steven Slocum
Tera Guru

Since you likely won't be resolving a catalog task, change the metric definition to instead calculate on the time spent as "active"

Name: Open_SCTASK

Table: sc_task

Field: Active

Type: Field Value Duration

Description: Special case that closes out (ends) other metrics when an the active flag in the sc_task turns false

 

 

Script:

// script can set answer to false to terminate processing of the metric
// mi - MonitorInstance
// answer
if (!current.active) {
  answer = false;
  mi.endDuration();
  closeDurations(mi.current);
}

function closeDurations(current) {
    var gr = new GlideRecord('metric_instance');
    gr.addQuery('id', current.sys_id);
    gr.addQuery('calculation_complete', false);
    gr.addQuery('definition.type', 'field_value_duration');
    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

6 REPLIES 6

We handle this separately from the metric itself. We are doing it from a business rule. We have a different business rule for each table because the state value used in the script conditions didn't perfectly align if we tried to put the BR on the Task table.

 

Name: Calculate Duration

Table: sc_task

When: (Advanced) Before Insert & Update

 

Script Condition: current.state == 3 || current.state == 9 //Use whatever your states are that result in an sc_task becoming inactive. i.e., Canceled, Completed, Closed, Closed Incomplete

 

(function executeRule(current, previous /*null when async*/) {

	setClosureFields();

function setClosureFields() {
	// RITM State is Closed so
	// 1. set the closed time to now if not supplied

	if (current.closed_at.nil())
		current.closed_at = gs.nowDateTime();
	
	// Update the fields that indicate the time/duration of the incident from open to close.
	// Keep track of duration as a glide_duration value (dd hh:mm:ss) and as a pure number of seconds.
	// Both calendar time and business time are maintained.
	
	var dataChange = current.opened_at.changes() || (current.closed_at.changes());
	var opened = current.opened_at.getDisplayValue();
	var closed = current.closed_at.getDisplayValue();
	
	if (dataChange || current.business_duration.nil())
		current.business_duration = gs.calDateDiff(opened, closed, false);
	
	if (dataChange || current.business_stc.nil())
		current.business_stc = gs.calDateDiff(opened, closed, true);
	
	if (dataChange || current.calendar_duration.nil())
		current.calendar_duration = gs.dateDiff(opened, closed, false);
	
	if (dataChange || current.calendar_stc.nil())
		current.calendar_stc = gs.dateDiff(opened, closed, true);
}


})(current, previous);

 

Apologizes on the late reply. Thanks for sharing this with me. I'm testing it in dev now. Thank you for helping me out on this. I need to start learning more javascript it seems like too for these advanced items.