- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-11-2016 08:30 AM
Howdy--
I have a record that has the following fields:
u_start_time (Date/Time)
u_end_time (Date/Time)
u_activity_duration (Duration)
In a Business Rule, I am trying to add u_activity_duration to u_start_time to calculate u_end_time.
I have tried the following:
(function executeRule(current, previous /*null when async*/) {
// calculates the end time of an activity based on the start time and duration
gs.log("Start time: " + current.u_start_time);
gs.log("Duration: " + current.u_activity_duration);
gs.log("Calculated end time: " + current.u_start_time.add(current.u_activity_duration));
current.u_end_time = current.u_start_time.add(current.u_activity_duration);
})(current, previous);
That results in the following logs:
Start time: 2016-07-11 15:23:48
Duration: 1970-01-01 00:59:00
Calculated end time: undefined
After searching these forums, I have tried many variations on the script above, including using getValue, getNumericValue, etc., all with the same results.
Any suggestions on how I can add the Start Time and Duration to calculate and End Time?
Thanks,
Jeremy.
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-11-2016 08:38 AM
Here you go
var gdt = new GlideDateTime(current.u_start_date.getDisplayValue());
var ms=current.u_activity_duration.dateNumericValue();
gdt.add(ms);
current.end_date=gdt.getValue();
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-11-2016 08:54 AM
Thanks Abhinay and Chuck!
Between both of your responses, I got it working.
Here's the working script I ended up with:
(function executeRule(current, previous /*null when async*/) {
// calculates the end time of an activity based on the start time and duration
var startTime = new GlideDateTime(current.u_start_time.getDisplayValue());
var duration = current.u_activity_duration.dateNumericValue();
startTime.add(duration);
gs.log("Calculated end time: " + startTime);
current.u_end_time = startTime.getDisplayValue();
})(current, previous);
It seems odd to me that you have to convert to "human readable" using getDisplayValue() rather than just working with the GlideDateTime and GlideDuration objects directly, but it's working now and I'm happy!
Best, and thanks again,
Jeremy.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-11-2016 08:56 AM
Thanks for posting your final solution and marking the appropriate response complete.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-11-2016 09:00 AM
At the end it should be either
current.u_end_time = startTime.getValue();
or
current.setDisplayValue('u_end_date',startTime.getDisplayValue());
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-11-2016 09:09 AM
Thanks, Abhinay!
It was working with getDisplayValue, but I changed to getValue and confirmed that it works that way, too. I'm guessing that getValue is "safe" across timezones and user settings while getDisplayValue might break?
J.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-11-2016 09:18 AM
Hi Jeremy,
In short -
getValue() retrieves the UTC time value
getDisplayValue() retrieves the system timezone value.