Metri definition that tracks incidents resolved directly from an "On Hold" state.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-26-2025 07:10 AM
Hi,
Need some help. I created the metric and it triggers correctly, but it’s not calculating the duration, updating the end date, or setting calculation_complete to true. How can I configure this to work properly?
Regards
CarolMa
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-26-2025 09:47 PM
Hello @CarolMa6,
-
1. Metric Definition:
- Type: Ensure the "Type" field in your metric definition is correctly set. If it's a field value duration, the system will automatically handle end date and duration. If it's a script calculation, ensure the script properly sets the end time, duration, and
calculation_complete
totrue
. - Start Condition: Verify your start condition is accurately identifying the point where the metric should begin.
- End Condition: Ensure your end condition is correctly identifying the point where the metric should end.
- Duration Calculation: If you're using a script calculation, ensure your script uses the
gs.dateDiff()
function to calculate the duration between the start and end times. You may also need to convert the result to milliseconds. - End Time: Ensure the script updates the
end
field of themetric_instance
with the current time (e.g.,gs.nowDateTime()
). - Calculation Complete: Set the
calculation_complete
field of themetric_instance
totrue
. - Save: After making changes, remember to save the metric definition.
- Type: Ensure the "Type" field in your metric definition is correctly set. If it's a field value duration, the system will automatically handle end date and duration. If it's a script calculation, ensure the script properly sets the end time, duration, and
-
2. Business Rule (if applicable):
- Trigger: Ensure the business rule triggers when the field that affects the metric is updated.
- Script: If you're using a script in the business rule, verify it's accurately creating or updating the
metric_instance
record with the correct values.
-
3. Metric Instance:
- End Date: The
end
field in themetric_instance
record should be updated to the current time when the metric ends. - Duration: The
duration
field should be populated with the calculated time difference between the start and end times. - Calculation Complete: The
calculation_complete
field should be set totrue
when the duration is calculated.
- End Date: The
(function executeRule(current, previous /*null when async*/) {
// Check if the metric instance already exists
var mi = new GlideRecord('metric_instance');
mi.addQuery('definition', current.metric_definition.sys_id);
mi.addQuery('id', current.sys_id);
mi.query();
if (mi.hasNext()) {
mi.next();
//Update the existing metric instance
mi.end = gs.nowDateTime();
mi.duration = gs.dateDiff(mi.start, mi.end, true); //true for seconds, false for duration
mi.calculation_complete = true;
mi.update();
} else {
// Create a new metric instance
mi = new GlideRecord('metric_instance');
mi.initialize();
mi.definition = current.metric_definition;
mi.table = current.table;
mi.id = current.sys_id;
mi.start = previous.sys_created_on; //or other appropriate start time
mi.end = gs.nowDateTime();
mi.duration = gs.dateDiff(mi.start, mi.end, true); //true for seconds, false for duration
mi.value = current.u_your_field; // or your relevant field
mi.calculation_complete = true;
mi.insert();
}
})(current, previous);
-
Check the System Log:Review the System Log for any errors or warnings related to the metric definition or business rule.
-
Test and Debug:Create a test record and manually trigger the metric to see if it's working correctly.
-
Use the Debugger:If you're having trouble with the script, use the debug options in the ServiceNow platform to step through the script and see what's happening.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-26-2025 10:01 PM
Did you check any of the existing OOTB metric definition script?
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-03-2025 06:37 AM
Yes, I did check an existing OOB script.
Created the below, now my issue is whenever the incident is on hold the metric is triggered and moving from on-hold to in progress also adds the metric. It should strictly add the metric when user moves the incident state from on-hold to resolved.
I tried updating the script with the below 2 lines then the metric does not work at all
Regards
CarolMa