Update Metric Instances
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
a month ago
I have a metric definition with start time as the WIP Date field. Earlier, this was not configured to get value and metric instances created using this field, and it said empty, which led to the wrong duration calculation.
Now I populate the field WIP Date using a fixed script.
The task now is to update the start time in metric instances with the correct WIP date being used in the respective field value in tickets. Update the exact start time in metric instances with the WIP date, recalculate the duration, and give the correct date.
Please let me know how to get this configuration.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
a month ago
Hi @KiranmaiP ,
You can write a fix script to update the metirc instance with WIP date.
// Example: Fix metric instances where start is wrong
var miGR = new GlideRecord('metric_instance');
miGR.addQuery('definition', '<your_metric_definition_sys_id>'); // Filter for the specific metric definition
miGR.query();
while (miGR.next()) {
var taskGR = new GlideRecord('your_task_table'); // e.g., incident, change_request
if (taskGR.get(miGR.document_key)) {
if (taskGR.u_wip_date) { // Assuming u_wip_date is your WIP Date field
var newStart = taskGR.u_wip_date.getGlideObject();
miGR.start = newStart;
miGR.update();
}
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
This is updating start time in metric instances, but no recalculating the duration. Upon updating the start time - The system should automatically calculate duration, please help
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Hi @KiranmaiP ,
Re-run metric processing engine
Easiest and cleanest approach: call the OOB API to recalculate metrics rather than manually updating.
var MetricBaseProcessor = new SNC.MetricBaseProcessor();
MetricBaseProcessor.recalculateMetric(miGR); // where miGR is metric_instance record
Works only if your instance has MetricBase plugin + APIs.
Manually update start and duration
If you want to directly compute it in a fix script:
var miGR = new GlideRecord('metric_instance');
miGR.addQuery('definition', '<your_metric_definition_sys_id>');
miGR.query();
while (miGR.next()) {
var taskGR = new GlideRecord('incident'); // or your table
if (taskGR.get(miGR.document_key)) {
if (taskGR.u_wip_date) {
var newStart = taskGR.u_wip_date.getGlideObject();
miGR.start = newStart;
// Recalculate duration: end - start
if (miGR.end) {
var duration = gs.dateDiff(newStart, miGR.end.getGlideObject(), true);
miGR.duration = duration * 1000; // duration in ms
}
miGR.update();
}
}
}
gs.dateDiff(start, end, true) returns seconds, so multiply by 1000 for milliseconds since metric_instance.duration is stored in ms.