- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-03-2025 02:47 AM
Why the business duration is resulting in 0 seconds where it should be 1 hour 19 minutes as I have used OOB
Thanks
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-03-2025 06:32 AM
check this link and it talks about using PlannedTaskAPI to calculate the duration
Calculate the business duration using a schedule
I ran the same script and in preferences set the timezone as GMT and it gave me the correct output
try {
var schedule = new GlideSchedule("08fcd0830a0a0b2600079f56b1adb9ae", );
var open = new GlideDateTime('2025-01-03 07:00:00');
var closed = new GlideDateTime('2025-01-03 09:19:00');
// Use closed_at for "mark_closed" business rule and resolved_at for "mark_resolved" business rule
var plannedTaskAPI = new SNC.PlannedTaskAPI();
var resp = plannedTaskAPI.calculateDuration(open.getDisplayValue(), closed.getDisplayValue(), '08fcd0830a0a0b2600079f56b1adb9ae');
var response = new JSON().decode(resp);
var duration = response.duration;
gs.info(duration);
} catch (ex) {
gs.info(ex);
}
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-03-2025 06:09 AM
why to use timezone in calculation?
You might be having timezone for your schedule already?
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-03-2025 06:20 AM - edited 01-03-2025 06:21 AM
@Ankur Bawiskar , there is no timezone selected for the OOB schedule and it is floating and if we don't need to provide the timezone still the business duration is resulting 0 seconds when the dates fall under schedule time. If I pass the userTZ as GMT it is working fine but for other scenerio it doesn't seem to be working. Am I missing something?
if (current.closed_at.nil()) {
var opened = new GlideDateTime(current.opened_at);
var closed = new GlideDateTime();
var userTZ = gs.getSession().getTimeZoneName();
var gsBusiness = new GlideSchedule("090eecae0a0a0b260077e1dfa71da828",userTZ );
var durBusiness = gsBusiness.duration(opened, closed);
gs.info("Dur " + durBusiness);
current.business_duration = durBusiness;
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-03-2025 06:25 AM
did you try with timezone as GMT?
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-03-2025 06:32 AM
check this link and it talks about using PlannedTaskAPI to calculate the duration
Calculate the business duration using a schedule
I ran the same script and in preferences set the timezone as GMT and it gave me the correct output
try {
var schedule = new GlideSchedule("08fcd0830a0a0b2600079f56b1adb9ae", );
var open = new GlideDateTime('2025-01-03 07:00:00');
var closed = new GlideDateTime('2025-01-03 09:19:00');
// Use closed_at for "mark_closed" business rule and resolved_at for "mark_resolved" business rule
var plannedTaskAPI = new SNC.PlannedTaskAPI();
var resp = plannedTaskAPI.calculateDuration(open.getDisplayValue(), closed.getDisplayValue(), '08fcd0830a0a0b2600079f56b1adb9ae');
var response = new JSON().decode(resp);
var duration = response.duration;
gs.info(duration);
} catch (ex) {
gs.info(ex);
}
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-05-2025 02:54 AM
Hi @Ankur Bawiskar , this is working fine for me, thank you so much.