- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-03-2022 02:47 AM
Hello,
i have written a after business rule on insert,update for my custom table
i'm getting difference between two date/time type of fields ,but its not getting populated in my duration type of field
i want it to get populated as (eg. 14 04:02:56)
i'm sharing business rule, tell what i have to change
(function executeRule(current, previous /*null when async*/ ) {
var fromdate = new GlideDateTime(current.getValue('u_start_date')); //to get start date value
var todate = new GlideDateTime(current.getValue('u_end_date')); //to get end date value
var output = gs.dateDiff(fromdate, todate);
gs.addInfoMessage(fromdate);
gs.addInfoMessage(todate);
gs.addInfoMessage(output);
current.u_required_duration= output.getByFormat();
})(current, previous);
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-07-2022 11:43 PM
Hi
Please make your BR as Before Insert/Update and update script as this
If you keep your BR as after then you will have to use current.update() in your BR which is not good.
(function executeRule(current, previous /*null when async*/ ) {
var fromdate = new GlideDateTime(current.getValue('u_start_date')); //to get start date value
var todate = new GlideDateTime(current.getValue('u_end_date')); //to get end date value
var duration = GlideDateTime.subtract(fromdate,todate);
current.u_required_duration = duration;
})(current, previous);
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-03-2022 02:54 AM
There is a GlideDuration API. Please see the docs.
You can also check some of the OOTB business rules for an example. e.g (Calculate Wrap Up Duration)
(function executeRule(current, previous /*null when async*/) {
if (current.isActionAborted() || current.state_changed_on.nil() || current.closed_at.nil()) {
return;
}
var wrap_up_start = current.state_changed_on.dateNumericValue();
var wrap_up_end = current.closed_at.dateNumericValue();
current.wrap_up_duration = new GlideDuration(wrap_up_end - wrap_up_start);
})(current, previous);
Please mark Correct and Helpful if my answer helps you resolve your issue. Thanks!
Martin Ivanov
Community Rising Star 2022
Please mark Correct and click the Thumb up if my answer helps you resolve your issue. Thanks!
Martin Ivanov
ServiceNow MVP 2023, 2024
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-03-2022 03:03 AM
Hi Akshay,
Please refer the below link , it will helpful for you.
Link - How to calculate difference between two dates and time
Thanks,
Vikas

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-07-2022 11:18 PM
Please mark Correct and Helpful if my answer helps you resolve your issue.
Тhis will close the thread and other users may also benefit from it.
Thanks!
Martin Ivanov
Community Rising Star 2022
Please mark Correct and click the Thumb up if my answer helps you resolve your issue. Thanks!
Martin Ivanov
ServiceNow MVP 2023, 2024
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-07-2022 11:43 PM
Hi
Please make your BR as Before Insert/Update and update script as this
If you keep your BR as after then you will have to use current.update() in your BR which is not good.
(function executeRule(current, previous /*null when async*/ ) {
var fromdate = new GlideDateTime(current.getValue('u_start_date')); //to get start date value
var todate = new GlideDateTime(current.getValue('u_end_date')); //to get end date value
var duration = GlideDateTime.subtract(fromdate,todate);
current.u_required_duration = duration;
})(current, previous);
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader