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

Just change below var queryString = "business_duration=NULL";

Hi Mike - turns out I was having a 'muppet' moment...  I had replaced the table name 'metric_instance' with the sys_id or name of the metric definition....

once I rectified that things were humming along...

 

Thanks again!

Do we need to add this in the script include -MetricInstance Or in the script section in Metric definition?

Hi Nikitia,

You run this script as a background script, one-off - navigate to 'System Definition>Scripts - Background' to run the script.

However I wanted to also run it regularly so I setup a scheduled job as per below:

 

find_real_file.png

 

Hope this helps!

 

Cheers

Carl.

Hi Mike!

I am trying to make sense of this solution. Is the "gr"-variable related to the metric instance?

I have a requirement to calculate "assigned to"-duration based on a schedule. Outside of the schedule hours the field value duration "clock" needs to be paused.

 Eg: An assignee accepts an incident and the metric is created with a "start"-value. The business day ends (based on defined schedule) and the next business day, the assignee reassigns the incident to another assignee, which sets the "end" value on the Metric Instance. In this case, I want to calculate only the assigned to duration within the business day hours.

Is this possible?