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

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

Thanks Mike - this worked a treat!  It is now calculating the business duration for all my metrics using fields.

One other question if I may - for all my historical records I now have counless records that still have an empty 'business_duration' field - is there a script I can run to update all these historical records to calculate the 'business_duration' value for all thee metric instances?

 

Cheers

Carl.

 

You can run background script

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

Hi Mike,

Thanks for the script, finally got round to testing this and it comes up with an error as per below:

Evaluator: java.lang.NullPointerException
   Caused by error in script at line 2

      1: var gr = new GlideRecord("Incident State Duration");
==>   2: gr.addEncodedQuery('end!=NULL^business_duration=NULL');
      3: gr.autoSysFields(false); // so that the records don't have system updates
      4: gr.query();
      5: while(gr.next()) {

I even tried something like the below for the first few lines:

var queryString = "end!=NULL^ORbusiness_duration=NULL";
var gr = new GlideRecord("Incident State Duration");
gr.addEncodedQuery(queryString);
gr.autoSysFields(false); // so that the records don't have system updates
...

but it also comes up with an error, as below:

Evaluator: java.lang.NullPointerException
   Caused by error in script at line 3

      1: var queryString = "end!=NULL^ORbusiness_duration=NULL";
      2: var gr = new GlideRecord("Incident State Duration");
==>   3: gr.addEncodedQuery(queryString);
      4: gr.autoSysFields(false); // so that the records don't have system updates
      5: gr.query();
      6: while(gr.next()) {

Are you able to figured out what is incorrect?

Thanks
Carl.