Glide duration for subtract is not working

venkatesh tadip
Tera Contributor

Hi Team,

 

I have an issue with glide duration in the change form

When I was subtracting two duration fields, I got output has undefined value.

Could you please help me out where my script was not working.

 

 

script

--------

var a = new GlideRecord("change_request");
a.addEncodedQuery("numberSTARTSWITHCHG0030008");
a.query();
if(a.next()){
var vdt = a.u_validationtime.getGlideObject();
var back = a.u_backouttime.getGlideObject();
var start = a.start_date.getGlideObject();
var end = a.end_date.getGlideObject();
var imp = gs.dateDiff(start.getDisplayValueInternal(), end.getDisplayValueInternal(), false);
var add = vdt.add(back);
var correctAdd = add.getDurationValue();
var sub = imp.subtract(correctAdd);
gs.print(imp +" "+correctAdd +" "+sub);
}
-------
output
Script: 1 00:00:05 10:00:00 undefined

 

 

3 REPLIES 3

Brad Bowman
Kilo Patron
Kilo Patron

Are any of these field types duration or all date/time?  There are different approaches to subtracting two actual duration fields vs date/time difference.

Hi Brad,
In the script start_date and end_date are the date/time type fields remaining are duration type fields.

The issue is that you are attempting to subtract the value of two duration fields that were added together from the value of two date/time fields that were subtracted.  One approach is to convert all fields to a numeric value, then add/subtract with ease, converting the final number to whatever format you need: 

var a = new GlideRecord("change_request");
a.addEncodedQuery("numberSTARTSWITHCHG0030008");
a.query();
if (a.next()) {
    var vdt = a.u_validationtime.dateNumericValue();
    var back = a.u_backouttime.dateNumericValue();
    var start = new GlideDateTime(a.start_date).getNumericValue();
    var end = new GlideDateTime(a.end_date).getNumericValue();
    var imp = end - start;
    var add = vdt + back;
    var sub = imp - add;
    gs.print(imp + " " + add + " " + sub);
}