Addition & subtraction of two duration field
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-04-2016 03:27 PM
Hi All,
Could anyone please let me know how to add or subtract two duration field? below are the line of codes:-
var gr = mi.getNewRecord();
gr.start = current.sys_created_on;
gr.end = current.sys_updated_on;
var duration = gs.dateDiff(gr.start.getDisplayValue(), gr.end.getDisplayValue());
var grd = new GlideAggregate('metric_instance');
grd.addQuery('id', current.sys_id);
grd.addQuery('definition','122f2b283c0a808ae7657b7132cd0a4f78');
grd.addAggregate('SUM', 'duration');
grd.query();
if(grd.next())
{
var grd_duration= grd.getAggregate('SUM', 'duration');
gs.log('Value of Duration:' + grd_duration); // This one is printing correct duration
duration = duration - grd_duration;
gs.log('Total Value :' + duration); // This one is printing as NaN.
}
gr.duration.setNumericDateValue(duration);
gs.log('GR Duration Value :' + gr.duration); // This one is printing as undefined.
gr.calculation_complete = true;
gr.insert();
Quick reply must be appreciated.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-04-2016 06:48 PM
Here you go
var gr = mi.getNewRecord();
gr.start = current.sys_created_on;
gr.end = current.sys_updated_on;
var duration = gs.dateDiff(gr.start.getDisplayValue(), gr.end.getDisplayValue(),true)*1000;
var grd = new GlideAggregate('metric_instance');
grd.addQuery('id', current.sys_id);
grd.addQuery('definition','122f2b283c0a808ae7657b7132cd0a4f78');
grd.addAggregate('SUM', 'duration');
grd.query();
if(grd.next())
{
var grd_duration= grd.getAggregate('SUM', 'duration');
var dur= new GlideDuration(grd_duration);
var agg_dur=dur.getNumericValue();
gs.log('Value of Duration:' + grd_duration); // This one is printing correct duration
duration = duration - agg_dur;
gs.log('Total Value :' + duration); // This one is printing as NaN.
}
gr.duration.setDateNumericValue(duration);
gs.log('GR Duration Value :' + gr.duration); // This one is printing as undefined.
gr.calculation_complete = true;
gr.insert();
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-04-2016 10:36 PM
Use the aggregate function SUM()
Thanks
Pradeep D J
PS: Hit like, Helpful or Correct depending on the impact of the response