- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-12-2017 01:04 PM
Hi,
I am unsuccessfully trying to dynamically set/calculate 'duration' field value based on difference between opened_at and closed_at fields in an scoped application.
I tried using script below for 'calculated value' but no luck.
Thanks in advance.
(function calculatedFieldValue(current) {
var startDate = current.opened_at;
var endDate = current.closed_at;
var duration = GlideDateTime.subtract(startDate, endDate);
return duration;
})(current);
I tried also:
return GlideDateTime.substract(opened_at,closed_at);
but no luck - -my calculated field show <empty> value.
Thank you.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-13-2017 09:29 AM
Can we try something like this
(function calculatedFieldValue(current) {
var startDate = new GlideDateTime(current.opened_at);
var endDate = new GlideDateTime(current.closed_at);
var duration = GlideDateTime.subtract(startDate, endDate);
return duration.getDisplayValue();
})(current);
Please mark my response as correct and helpful if it helped solved your question.
-Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-12-2017 02:15 PM
Hello Valon
This post might be helpful for you
How to calculate difference between two dates in hours ?
Please mark my response as correct and helpful if it helped solved your question.
-Thanks

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-12-2017 02:18 PM
Please try with GlideDuration, let's see if this helps: Scoped GlideDuration API Reference - ServiceNow Wiki
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-13-2017 08:59 AM
Thanks Shishir,
Unfortunately no luck.
I want to use the following fields: opened_at and closed_at and calculate duration which would go on my_test_field
I appreciate any comment
Used the following code in "Calculated Value" script but no luck
(function calculatedFieldValue(current) {
var duration = new GlideDuration(current.opened_at);
var duration2 = new GlideDuration(current.closed_at);
var answer = duration2.subtract(duration);
return answer;
})(current);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-13-2017 09:29 AM
Can we try something like this
(function calculatedFieldValue(current) {
var startDate = new GlideDateTime(current.opened_at);
var endDate = new GlideDateTime(current.closed_at);
var duration = GlideDateTime.subtract(startDate, endDate);
return duration.getDisplayValue();
})(current);
Please mark my response as correct and helpful if it helped solved your question.
-Thanks