Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

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

Ankur Bawiskar
Tera Patron
Tera Patron

@Sneha_18 

is this a OOB BR script or a custom one?

Did you check OOB duration BR mark_closed on incident table?'

This KB will help you for business duration

How the Duration and Business Duration calculation works 

something like this

var schedule = new GlideSchedule("38f8b6d2c0a801640075da0e39d47696"); 
 
// Use closed_at for "mark_closed" business rule and resolved_at for "mark_resolved" business rule 
var duration = schedule.duration(current.opened_at.getGlideObject(), current.closed_at.getGlideObject()); 
 
// Stores value as a GlideDuration 
current.business_duration = duration; 

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

Hi @Ankur Bawiskar ,

I have created a business rule to calculate the business duration for the cases created for different timezone users for working hours as 8 am to 5 pm in their respective timezone using OOB schedule "8-5 weekdays excluding holidays" but it is populating as "0 seconds" even if the time falls under the schedule time. 

@Sneha_18 

cases created for different timezone -> what do you mean by this?

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

@Ankur Bawiskar I mean the cases opened by, thanks!