
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-02-2023 12:17 PM
We have a requirement to set the Business Duration based on a schedule.
I have used the Duration Calculator to calculate the correct work time and total times in seconds just as the documentation explains. However, when setting the business_duration field it shows "0 seconds" even though the same variable outputs the correct number of seconds via the logs.
This is the code I'm using to try and set the business_duration field.
gs.include('DurationCalculator');
var dc = new DurationCalculator();
dc.setSchedule('sys_idOfSchedule');
var dcDuration = dc.calcScheduleDuration(current.opened_at, current.closed_at); //actual work seconds
var workTime = dc.getSeconds(); //actual work seconds
var totalTime = dc.getTotalSeconds(); //total seconds including work
var workDate = new Date(workTime*1000); //date (1/1/1970 + work seconds*1000) tried to use this to no avail
current.setValue('business_duration', workTime);
gs.log("calcScheduleDuration with schedule SC TASK"+"(" + current.number + ") - WorkTime "+ workTime + " and Total Time "+totalTime+ " and Work Date "+workDate+ " and DC Duration "+dcDuration);
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-03-2023 09:16 AM
This is insane, that I can't pull data from the duration calculator and populate the duration field without using substring()... but here it is.
Duration fields save as date/time. They start at the default date of 1/1/1970, which is why we were using Date(). BUT, we finally learned that it will not accept date.toStringISO()... but it will accept this format "1970-01-20T11:22:15."
So this is what I added to the bottom in order for it to populate this field and it works:
var workDate = new Date(workTime*1000).toISOString(); //date (1/1/1970 + work seconds*1000)
var workDateString = workDate.substring(0,19);
current.setValue('business_duration', workDateString);// MUST HAVE THIS STRUCTURE '1970-01-20T11:22:15'
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-02-2023 11:59 PM
@sharepointau try like this
var startDate = new GlideDateTime('2020-4-17 00:00:00');
var endDate = new GlideDateTime('2020-4-21 00:00:00');
var schedule = new GlideSchedule();
schedule.load('08fcd0830a0a0b2600079f56b1adb9ae'); // loads "8-5 weekdays" schedule. ('08fcd0830a0a0b2600079f56b1adb9ae' is sysid of the schedule.)
var duration = schedule.duration(startDate, endDate);
gs.info(duration.getDurationValue()); // gets the elapsed time in schedule
Result of the code
18:00:00
Thanks,
Bharath
Bharath Chintala

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-03-2023 05:25 AM
Thank you for your reply, but we are wanting to use the Duration Calculator as it is working so well to calculate these numbers. I think our issue is with how to set the value of the business_duration field.
I did try to use the "getDurationValue()" to set the field but it returned "undefined" when used like this "dcDuration.getDurationValue();"

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-03-2023 05:26 AM
Thank you for this reply. We are wanting to use the Duration Calculator.
I did try to use dcDuration.getDurationValue(); but it returned "undefined"
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-03-2023 01:31 AM
Hello,
You need to convert the number to a string before setting the value of the field.
Try something like this and let us know the outcome
current.setValue('business_duration', workTime.toString());
Regards
Prasad