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

mnaputano1
Tera Contributor

Hi Carl and Mike,

    I am trying to accomplish the same thing you are doing with calculating the business duration for some of the incident metrics that are available out of the box. I don't understand why the field is business duration field is available on the table for metrics, yet it is not being calculated out of the box!

I see you marked this as an accepted solution, but I am not following how you were able to do this. Did you create a business rule that calculates the business duration, or another script include or something? If you wouldn't mind sharing your solution, that would be greatly appreciated!

Thank you,

Mike

Business duration is not calculated OOTB so you can create business rule or scheduled script to do so. 

Below is scheduled job script that you can use to fill in business duration (make sure to replace sysid of schedule)

 

var gr = new GlideRecord("metric_instance");
gr.addEncodedQuery('end!=NULL^business_duration=NULL');
gr.autoSysFields(false); // so that the records don't have system updates
gr.query();
while(gr.next()) {
	var gsBusiness =new GlideSchedule('dcf6c8fedb5d7e007d1bfa0dbf9619db');
	// Get duration based on schedule
	gr.business_duration = gsBusiness.duration(gr.start.getGlideObject(), gr.end.getGlideObject());
	gr.setWorkflow(false); 
	gr.update();
}

 

Thank you so much Mike!!

This is great, how would I limit the records updated to records over the last year?  I don't want this to take a long time and tie up my instance.  Any help is appreciated.

VShine
Mega Guru

@Mike Patel 

Can I use this script for HR Application. I am trying to calculate Business duration for HR case. When using above script in BR, I am getting this error

find_real_file.png

 

Here is BR

find_real_file.png