GlideDateTime.subtract doesn't work

abrouf
Kilo Sage

I used the following commands on default value to calculate the number of days subtracting current date to due date but didn't work. Any comment or advise is greatly appreciated.

javascript:calendar_duration = GlideDateTime.subtract(due_date.getDisplayValue(), gs.nowDateTime());

Following this syntax:

<duration_field> = GlideDateTime.subtract(<start_field>.getDisplayValue(),gs.nowDateTime());

21 REPLIES 21

Hi Everybody,


Thank you for your quick responses. I tested all options, no luck so far. Here is my output:


find_real_file.png


Hi Rouf,



I tried using the below code in my instance, which update the duration field.



var gr = new GlideRecord("incident");


gr.addQuery("sys_id","50dcd0704f20320062b8b4a18110c71a");


gr.query();


if(gr.next())


{


var start = new GlideDateTime(gr.opened_at);     //opened field value


var end = new GlideDateTime();  


var dur = GlideDateTime.subtract(start, end); // returns a GlideDuration object


gr.calendar_duration = dur; //setting the duration field


gr.update();


}


You just have to replace the field values:



var gr = new GlideRecord("problem");


gr.addQuery("sys_id","50dcd0704f20320062b8b4a18110c71a");


gr.query();


if(gr.next())


{


var start = new GlideDateTime(gr.due_date);     //due date field value


var end = new GlideDateTime();  


var dur = GlideDateTime.subtract(start, end); // returns a GlideDuration object


gr.u_duration = dur; //setting the duration field


gr.update();


}


Hi Gowrisankar,


Thank you for sharing the script. I used it on "Default Value", "Calculated Value" (used calculatedFieldValue), also as client script but no luck for me, may be I am missing something.I replaced sys_id and fields as follow:



var gr = new GlideRecord("problem");


gr.addQuery("sys_id","0436d9d8db43430074caf08c0c961945");


gr.query();


if(gr.next())


{


var start = new GlideDateTime(gr.due_date);     //due date field value


var end = new GlideDateTime();  


var dur = GlideDateTime.subtract(start, end); // returns a GlideDuration object


gr.calendar_duration = dur; //setting the duration field


gr.update();


}


Hi Rouf,



Can you run that script in background.



Also can you tell me what exactly is the type of field 'due_date'?