- 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
‎05-16-2018 06:53 AM
Hi guys, thanks for this info - would be really grateful if someone could take a look at my similar recent post here:
https://community.servicenow.com/community?id=community_question&sys_id=6aa6a486db0a57c058dcf4621f9619d5&anchor=answer_dacf95dedbc2df045322f4621f96198a
Although I am trying to achieve this onChange, I have tried using the above business rule without much luck. 'My' business rule is below FYI:
(function executeRule(current, previous /*null when async*/) {
// calculates the end time of an activity based on the start time and duration
var start = new GlideDateTime(current.start_date.getValue());
var duration = current.u_change_duration.dateNumericValue();
start.add(duration);
//gs.log("Calculated end time: " + start);
current.end_date = start.getValue();
})(current, previous);
Thanks in advance.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-25-2021 12:21 PM
I tried the accepted solution but it didn't work for me so I ended up having to modify the script a bit. It came down to converting the duration field into GlideDateTime and then using .getValue(). Here's my working business rule (before insert/update):
Condition:
current.variables.access_end == '' || current.variables.access_start.changes() || current.variables.how_long.changes()
Script:
(function executeRule(current, previous /*null when async*/ ) {
// Add your code here
// calculates the end time of an activity based on the start time and duration
var startTime = new GlideDateTime(current.variables.access_start.getValue()); // getting start time
var dur = new GlideDateTime(current.variables.how_long.getValue()); // getting duration and converting to GlideDateTime
dur = dur.getNumericValue()/1000; // calculating the total duration in seconds
//gs.addInfoMessage(dur);
startTime.addSeconds(dur); // add the seconds to the start time to calculate end time
current.variables.access_end = startTime.getValue(); // set the end time
//gs.addInfoMessage(startTime);
gs.addInfoMessage("End time automatically adjusted based on start time and duration");
})(current, previous);