Calculate duration for the metric instance
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-21-2025 09:46 AM
Hi Team,
I have a requirement that when an incident ticket is resolved one metric will be assigned. But the same metric need to be assigned when the incident ticket is reopened and resolved again and end time need to update with the last updated time(when the incident ticket is resolved).
I have written business rule to populate the Metric once again when the incident is reopened and resolved but unable to populate the duration value. Can you please help me to achieve this.
Below script which I have written:
I have created a business rule to populate the Metric whenever we reopen and resolve the ticket. It is working as expected but the duration field is not getting updated.
Whenever I am using the commented script, the metric is not getting assigned. Can you please help me the resolve this issue.
if (previous.state != 6 && current.state == 6) {
// check if it was reopened before resolving
if (current.reopen_count > 0) {
var metric = new GlideRecord('metric_instance');
metric.initialize();
metric.name = 'Create to Resolve Duration';
metric.definition = '35edf981c0a808ae009895af7c843ace'; //metric definition sys_id
metric.id = current.sys_id;
var now = new GlideDateTime();
metric.start = current.sys_created_on;
metric.end = now;
//var sched new GlideSchedule('ba7530901bcf2450988887f7cc4bcb8b');
//var dur = sched.duration(metric.start,metric.end);
//metric.duration dur.getDurationValue();
metric.value = current.incident_state.getDisplayValue();
metric.calculation_complete = true;
metric.insert();
gs.info("Duration Set:" + metric.duration + "| Inserted: " + metric.sys_id);
gs.info("Metric created for " + current.number + " after reopen.");
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-21-2025 09:57 AM
Can you try this if this works for you.
if (previous.state != 6 && current.state == 6) {
// Check if it was reopened before resolving
if (current.reopen_count > 0) {
var metric = new GlideRecord('metric_instance');
metric.initialize();
metric.name = 'Create to Resolve Duration';
metric.definition = '35edf981c0a808ae009895af7c843ace'; // Metric definition sys_id
metric.document = current.sys_id; // Use 'document' not 'id' field to link to incident
metric.table = 'incident';
var start = current.sys_created_on;
var end = new GlideDateTime();
metric.start = start;
metric.end = end;
// Calculate duration using business schedule if needed
var schedule = new GlideSchedule('ba7530901bcf2450988887f7cc4bcb8b'); // Your schedule sys_id
var dur = schedule.duration(start, end);
// You need to call getDurationValue() to extract the duration in seconds
metric.duration = dur.getDurationValue();
metric.value = current.incident_state.getDisplayValue();
metric.calculation_complete = true;
metric.insert();
gs.info("Duration Set: " + metric.duration + " | Metric Inserted for Incident: " + current.number);
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-21-2025 11:57 PM
Hi Sidak,
Thank you for the the help.
I have tried the above script. In this case the metric(Created by BR) itself not getting attached.
Best Regards,
Nageshwari P.