- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-05-2024 08:30 AM
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
I would expect the get 7 for this record
Thanks in advance!
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-05-2024 09:48 AM
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()

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-05-2024 09:48 AM
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()
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-05-2024 11:00 AM
Wooooooh..!
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-05-2024 10:50 AM
Thank you!