Issue with Calculating Average mi_business_duration for Incident Using Custom Schedule in ServiceNow
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-18-2024 12:55 PM
I'm encountering an issue with a metric definition in ServiceNow and would appreciate any insights or guidance you can provide.
What I've Done So Far:
I have successfully created a metric definition to measure the average mi_duration for an incident from the time it is created until it is marked "In Progress". The script for this metric is working as intended.
Here is the script:
// variables available
// current: GlideRecord - target incident
// definition: GlideRecord - (this row)
var s = current.incident_state;
if (s == 2)
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 = gs.nowDateTime();
gr.duration = gs.dateDiff(gr.start.getDisplayValue(), gr.end.getDisplayValue());
gr.calculation_complete = true;
gr.insert();
}
The Issue:
Now, I am trying to calculate the average mi_business_duration using a custom schedule (8-5 weekdays excluding holidays) with the specific sys_id=090eecae0a0a0b260077e1dfa71da828. However, the script I've written for this purpose does not seem to be functioning correctly when I run the report.
Here is the script I am having issues with:
// variables available
// current: GlideRecord - target incident
// definition: GlideRecord - (this row)
var s = current.incident_state;
if (s == 2) {
createMetric();
}
function createMetric() {
var mi = new MetricInstance(definition, current);
if (mi.metricExists()) {
return; // If the metric already exists, do not create a duplicate
}
var gr = mi.getNewRecord();
gr.start = current.sys_created_on; // The time the incident was created
gr.end = gs.nowDateTime(); // The current time
// Calculate the business duration using the specified schedule
var schedule = new GlideSchedule('090eecae0a0a0b260077e1dfa71da828'); // Load the schedule with the provided sys_id
var duration = schedule.duration(gr.start, gr.end); // Calculate the duration based on the loaded schedule
// Set the calculated duration in the business duration field
gr.setValue('mi_business_duration', duration.getNumericValue());
// Calculate the overall duration irrespective of the business hours
gr.duration = gs.dateDiff(gr.start.getDisplayValue(), gr.end.getDisplayValue());
// Mark the calculation as complete
gr.calculation_complete = true;
// Insert the new metric record into the database
gr.insert();
}
After running the report, I get no results.
Thank you in advance for your help!