
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-04-2018 05:57 PM
Hi Team,
The OOTB Metric Instances for 'Field Value Duration' only calculate the duration and not the 'business duration'.
These use the 'MetricInstance' Script Include to set the required values and I can't figure out how to add the 'business duration' field.
The code 'gr.business_duration = gs.dateDiff(gr.start.getDisplayValue(), gr.end.getDisplayValue());' will allow me to actually save a value in the 'business_duration' field, but obviously it's not he correct value.
I need to load a schedule and wanted to use code received from Dennis (on another of my posts) but I can't seem to get it working at all. Code I'm trying is below:
var gdurCalendar = GlideDateTime.subtract(gr.startgetDisplayValue(), gr.end.getDisplayValue()); // Calculate calendar duration
var gdurBusiness = new GlideDuration(gdurCalendar); // Calculate business duration - If we can't find a valid schedule, use the calendar duration
var gsBusiness = new GlideSchedule('dcf6c8fedb5d7e007d1bfa0dbf9619db');
gdurBusiness = gsBusiness.duration(gr.start.getDisplayValue(), gr.end.getDisplayValue());
gr.business_duration = gdurBusiness;
Any help is appreciated as I'm probably mixing up a bunch of stuff as I'm not an advanced coder.
I've also logged a ticket with HI as research shows this may be a bug as well.
Thanks Carl.
Solved! Go to Solution.
- Labels:
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-04-2018 06:07 PM
try
// Create schedule
var gsBusiness =new GlideSchedule('dcf6c8fedb5d7e007d1bfa0dbf9619db');
// Get duration based on schedule
gr.business_duration = gsBusiness.duration(gr.start.getGlideObject(), gr.end.getGlideObject());

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-24-2020 03:34 PM
when calling your 'getGlodeObject' have you tried adding a 'global.getGlideObject' to the front, to put it in Global scope for the call? Or maybe global.start.getGliveObject'?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-24-2020 03:51 PM
No, I have use your code as is. Where should I be making change?

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-24-2020 04:40 PM
My code is used inside the Metric itself, not a separate Business Rule, see below:
/* CJF Added metric to capture how log it takes to complete a HR Case - copied from Incident Script
* Creates or updates the HR Case Completion metric based on the time a case
* is opened until it is Completed (state is 2).
* Calculates the 'Business Duration' based on the 'New Zealand Schedule'
*/
(function evaluateMetric(current, definition) {
var scheduleName = 'New Zealand Schedule';
// Try to fetch an existing metric, if one exists
var grMetric = new GlideRecord('metric_instance');
grMetric.addQuery('id', current.getUniqueValue());
grMetric.addQuery('definition', definition.getUniqueValue());
grMetric.query();
if (!grMetric.next()) {
// If the metric doesn't exist, create it
grMetric = new GlideRecord('metric_instance');
grMetric.newRecord();
grMetric.table = 'sn_hr_core_case';
grMetric.field = 'state';
grMetric.definition = definition.getUniqueValue();
grMetric.id = current.getUniqueValue();
grMetric.start = current.opened_at;
grMetric.calculation_complete = false;
}
// Regardless of whether this is a new record or an existing one, we want
// to update the field values to reflect the current state.
grMetric.field_value = current.getValue('state');
grMetric.value = current.getDisplayValue('state');
if (current.state == '2') { // Completed
var gdtStart = new GlideDateTime(current.opened_at);
var gdtEnd = current.closed_at.nil()
? new GlideDateTime()
: new GlideDateTime(current.closed_at);
// Calculate calendar duration
var gdurCalendar = GlideDateTime.subtract(gdtStart, gdtEnd);
// Calculate business duration
var gdurBusiness = new GlideDuration(gdurCalendar);
// If we can't find a valid schedule, use the calendar duration
// If you know the sys_id of your schedule, it would probably be a bit
// more efficient and less risky (in case your schedule name changes)
// to replace the following five lines and use it directly in a couple
// of lines such as:
//
// var gsBusiness =
// new GlideSchedule('0123456789abcdef0123456789abcdef');
// gdurBusiness = gsBusiness.duration(gdtStart, gdtEnd);
var grBusiness = new GlideRecord('cmn_schedule');
if (grBusiness.get('name', scheduleName)) {
var gsBusiness = new GlideSchedule(grBusiness.getUniqueValue());
gdurBusiness = gsBusiness.duration(gdtStart, gdtEnd);
}
grMetric.calculation_complete = true;
grMetric.end = gdtEnd;
grMetric.duration = gdurCalendar;
grMetric.business_duration = gdurBusiness;
}
grMetric.isNewRecord() ? grMetric.insert() : grMetric.update();
})(current, definition);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-27-2020 07:41 AM
We decided to go out with first release with Duration for now and then make updates to Business duration once scheduled are defined. Thank you so much for your help.