- 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-13-2017 10:57 AM
Thanks so much Prateek,
That worked!
However, I am having issues calculating duration correctly when endDate variable is <empty>
Any idea how to overcome that? I.e. by setting endDate variable to today's date? But, don't know how to do that?
Thanks again
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-13-2017 12:18 PM
Hello Valon
Try this
(function calculatedFieldValue(current) {
var startDate = new GlideDateTime(current.opened_at);
var endDate = new GlideDateTime(current.closed_at);
if(endDate == ''){
var today = new GlideDateTime();
var result = GlideDateTime.subtract(startDate, today);
return result.getDisplayValue();
}
else{
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-13-2017 12:26 PM
That worked !!!
Thanks so much.
V.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-13-2017 12:27 PM
Glad that it worked for you
Cheers!
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
12-11-2017 06:41 PM
Hello Prateek,
I used the same script but experiencing errors. Your input is appreciated.
Scripts:
(function calculatedFieldValue(current) {
var startDate = new GlideDateTime(current.u_today_s_date);
var endDate = new GlideDateTime(current.u_rca_due_date);
if(endDate == ''){
var today = new GlideDateTime();
var result = GlideDateTime.subtract(startDate, today);
return result.getDisplayValue();
}
else{
var duration = GlideDateTime.subtract(startDate, endDate);
return duration.getDisplayValue();
}
})(current);
return ''; // return the calculated value
})(current);
----------------------------------------------------------------
Evaluator: org.mozilla.javascript.EcmaError: Cannot read property "u_today_s_date" from null
Caused by error in script at line 4
Here is my configuration & date: