NEW METRIC DEFINITION APPEARS TO NOT WORK

randytangco
Mega Guru

Hello. I need help because I know I am not doing something correctly.  I created a new metric definition called "Create To Staff Assignment" where the goal is to record metric record from the time the INC is created and up to the time the assigned to field is valued.

 

Below is the metric definition.  The following code below the screen shot is the business rule.  As shown in the lower part of the screen shot of the metric definition record, the metric created records the start date of when I updated the INC to add an assigned to value.  I am wondering what am I missing given that there was already a metric definition for the same field I am trying to measure with the same type as shown by the screen shot after the new metric definition I created.

 

Would love to get some help to make this work.  Thank you all.

New Metric Definition Record

randytangco_0-1726664144446.png

Metric Definition List on INC

randytangco_4-1726664524755.png

 

 

Business Rule

randytangco_1-1726664205782.png

randytangco_2-1726664229073.png

randytangco_3-1726664266942.png

Script in the business rule

 

(function executeRule(current, previous /*null when async*/) {
// Ensure the business rule triggers correctly when assigned_to changes from empty to filled
if (JSUtil.nil(current.assigned_to) || !JSUtil.nil(previous.assigned_to)) {
return; // Only proceed if assigned_to was previously empty and is now filled
}

// Get the creation date of the incident (start time)
var createdDateTime = new GlideDateTime(current.sys_created_on);
 
// Get the current time (assignment time, i.e., end time)
var assignmentDateTime = new GlideDateTime(); // Current time at assignment

// Validate the GlideDateTime objects
if (!createdDateTime.isValid() || !assignmentDateTime.isValid()) {
gs.log("Assigned To Metric Generator Error: Invalid date times.");
return;
}

// Calculate the duration in milliseconds
var durationMillis = assignmentDateTime.getNumericValue() - createdDateTime.getNumericValue();

// Calculate the duration in days, hours, minutes, and seconds
var totalSeconds = Math.floor(durationMillis / 1000);
var days = Math.floor(totalSeconds / (24 * 3600));
totalSeconds %= (24 * 3600);
var hours = Math.floor(totalSeconds / 3600);
totalSeconds %= 3600;
var minutes = Math.floor(totalSeconds / 60);
var seconds = totalSeconds % 60;

// Log the calculated duration for debugging purposes
gs.log("Assigned To Metric Generator: Duration - " + days + " days, " + hours + " hours, " + minutes + " minutes, " + seconds + " seconds.");

// Create a new metric instance record
var metric = new GlideRecord('metric_instance');
metric.initialize();
metric.field = "assigned_to";
metric.name = "Create To Staff Assignment";
metric.value = current.getDisplayValue('assigned_to');
metric.start = createdDateTime; // Set the start time to the incident's created date
metric.end = assignmentDateTime; // Set the end time to the current time
metric.duration = durationMillis; // Set the duration in milliseconds
metric.calculation_complete = true; // Set calculation complete to true
metric.insert();
 
gs.log("Assigned To Metric Generator: Metric record created for incident: " + current.sys_id);

})(current, previous);
7 REPLIES 7

randytangco
Mega Guru

@Anantha Gowrara .  Just another input.  If I use the type script calculation, the metric_instance record is NOT created.  If I set the type to field value calculation, the metric_instance record is created with the start date filled in and the rest is empty.  It appears like there is an event waiting for the INC to be resolved or closed?  Where do I find this?

// variables available
// current: GlideRecord -  target incident
// definition: GlideRecord -  (this row)
var createdDate = new GlideDateTime(current.sys_created);

// Find the first assignment
var firstAssignment = new GlideRecord('incident');
firstAssignment.addQuery('sys_id', current.sys_id);
firstAssignment.addQuery('assigned_to', '!=', '');
firstAssignment.setLimit(1);
firstAssignment.query();

// If a first assignment is found, calculate the duration
if (firstAssignment.next()) {
//    var assignedDate = new GlideDateTime(firstAssignment.assigned_to);
//    var tDuration = GlideDateTime.subtract(assignedDate, createdDate);
//    current.metric_value = tDuration.getDecimal();
	createMetric();
}

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

Randy TCTS
Tera Expert

I made it work with the following code

 

 

 

// variables available
// current: GlideRecord -  target incident
// definition: GlideRecord -  (this row)
var createdDate = new GlideDateTime(current.sys_created);

// Find the first assignment
var firstAssignment = new GlideRecord('incident');
firstAssignment.addQuery('sys_id', current.sys_id);
firstAssignment.addQuery('assigned_to', '!=', '');
firstAssignment.setLimit(1);
firstAssignment.query();

// If a first assignment is found, calculate the duration
if (firstAssignment.next()) {
//    var assignedDate = new GlideDateTime(firstAssignment.assigned_to);
//    var tDuration = GlideDateTime.subtract(assignedDate, createdDate);
//    current.metric_value = tDuration.getDecimal();
	createMetric();
}

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