calculate Duration and Business Duration in scoped application

Sneha_18
Tera Contributor
Sneha_18_1-1735900877749.png

 

 
Sneha_18_0-1735900811208.png

Why the business duration is resulting in 0 seconds where it should be 1 hour 19 minutes as I have used OOB 

8-5 weekdays excluding holidays schedule where the start time as 8. 
Thanks

 

1 ACCEPTED SOLUTION

@Sneha_18 

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);
}

AnkurBawiskar_0-1735914605128.png

 

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

11 REPLIES 11

@Sneha_18 

Glad to know.

Can you share the final script so that it helps other community members?

How are you setting the timezone when the BR/script run since I just updated the timezone in User preferences and it gave correct output?

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Juhi Poddar
Kilo Patron

Hello @Sneha_18 

The possible reason could be that the schedule with the ID "090eecea0a0b260077e1dfa71da828" does not exist or is not correctly configured. Please verify this by navigating to System Definition > Schedules in ServiceNow and ensuring the schedule is present and properly set up.

 

"If you found my answer helpful, please like and mark it as an "accepted solution". It helps future readers to locate the solution easily and supports the community!"

 

Thank You
Juhi Poddar