GlideDateTime.subtract doesn't work
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-10-2017 05:58 PM
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());
- Labels:
-
Personal Developer Instance
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-10-2017 10:25 PM

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-10-2017 10:57 PM
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();
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-10-2017 10:59 PM
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();
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-10-2017 11:49 PM
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();
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-11-2017 01:10 AM
Hi Rouf,
Can you run that script in background.
Also can you tell me what exactly is the type of field 'due_date'?