Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

How to check if duration value is more than X days

Eli Guttman
Tera Guru

Hi,

I'm working with metrics and would like to check if the duration of certain metric is more than 3 days.

 

Using this script: 

var met = new GlideRecord('metric_instance');
if (met.get('4d873c0b47d186d0bfbeacb1026d4395')){
	var myduration = new GlideDuration(met.duration);
gs.info('Duration: getDayPart: ' + myduration.getDayPart());	
}

Returns 

 

*** Script: Duration: getDayPart: 19848

For this metric record

EliGuttman_0-1714922911197.png

I would expect the get 7 for this record

 

Thanks in advance!

 

1 ACCEPTED SOLUTION

Kieran Anson
Kilo Patron

You can't directly pass in a GlideElement (which met.duration is) into a GlideDuration constructor. Instead you can do the following:

var metricInstanceGR = new GlideRecord('metric_instance');
metricInstanceGR.get('9ab92906831102105e43c4a6feaad3c3');
metricInstanceGR.getElement('duration').getGlideObject().getDayPart()

 

 

View solution in original post

3 REPLIES 3

Kieran Anson
Kilo Patron

You can't directly pass in a GlideElement (which met.duration is) into a GlideDuration constructor. Instead you can do the following:

var metricInstanceGR = new GlideRecord('metric_instance');
metricInstanceGR.get('9ab92906831102105e43c4a6feaad3c3');
metricInstanceGR.getElement('duration').getGlideObject().getDayPart()

 

 

Wooooooh..! 

 

@Kieran Anson,

 

Even I was working in the same and I ended up with different code. But your code looking so precise and perfect. Really cool man. Because I never heard about getGlideObject() before. By the way thank you for this great response. I learnt something new today.

 

Cheers🥂

 

Here is my code,

var met = new GlideRecord('metric_instance');
if (met.get('04c7f6cb2f9bf9501592235df699b643')) {
    var startDuration = new GlideDateTime(met.start);
    var endDuration = new GlideDateTime(met.end);
    var newDuration = GlideDuration.subtract(startDuration,endDuration);
    gs.info(newDuration.getDayPart());
}

 

Regards,

Dhanraj.

Eli Guttman
Tera Guru

Thank you!