Calculating Business Duration on 'Field Value Duration' Metric Instance

Carl Fransen1
Tera Guru

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.

1 ACCEPTED SOLUTION

Mike Patel
Tera Sage

try

// Create schedule 
var gsBusiness =new GlideSchedule('dcf6c8fedb5d7e007d1bfa0dbf9619db');
// Get duration based on schedule
gr.business_duration = gsBusiness.duration(gr.start.getGlideObject(), gr.end.getGlideObject());

View solution in original post

18 REPLIES 18

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'?

No, I have use your code as is. Where should I be making change?

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);

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.