
- 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-03-2023 05:34 AM
Thank you for your reply. When using this it returned the same result of "0 Seconds."
Any thoughts on why it thinks a non-negative number is zero?
In the screenshot provided, there are 3 business_duration fields. The top is sc_task (the one I'm working with), RITM, and the third is request. In the case of this test the Task and Request business_durations should be the same.
The one with "0 Seconds" is using the above code.
The one with "20 Days 6 Hours 16 Minutes" is using the OOTB "gs.calDateDiff();"

- 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
01-28-2025 04:32 AM
I had the same issue and came up with a slightly different solution without needing a substring.
You can use GlideDateTime.setNumericValue( ) and as a parameter pass the seconds from the durationCalculator.getSeconds result multiplied by 1000 because setNumericValue expects milliseconds. And then a GlideDateTime object can be saved to a business duration field as value.
var durationCalcUtil = new DurationCalculator();
durationCalcUtil.setSchedule(gs.getProperty('<system_property>'));
durationCalcUtil.calcScheduleDuration(current.sys_created_on, current.closed_at);
var durationMilliseconds = durationCalcUtil.getSeconds() * 1000;
var durationGDT = new GlideDateTime();
durationGDT.setNumericValue(durationMilliseconds);
current.setValue('business_duration', durationGDT.getValue());