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

Anantha Gowrara
Kilo Sage

Hi @randytangco For such scenarios where you need to create metric instance to start from incident creation to time it is assigned, you should go with type as "Script calculation" and write scrip within definition to update start and end date of metric instance. Something like below.

 

AnanthaGowrara_1-1726666372618.png

 

 

 

Thank you for the response @Anantha Gowrara .  Would this mean I can turn off the business rule?

can i use the below same code in the metric script record?

 

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

i tried adding the script in the metric definition and the metric record is not created.  i tried doing it with the business rule disabled and enabled.