Calculating Business Duration Issues

mike_m35
Tera Contributor

Hello, we have a business rule that calculates the business duration based on a set schedule (working hours from 06:30 - 18:30 CST). This, for the most part has been working correctly, but I'm noticing some issues if the ticket is created before or after working hours. In this case, the business duration will calculate to 0 seconds and not account for the actual business time. The parent scheduled referenced in the script is on the US/Central timezone as well as our system timezone the same, unsure of why it's unable to calculate some of the business durations properly. 

 

mike_m35_1-1735070829985.png

the BR script is an after update when the state moves to closed or cancelled

 

(function executeRule(current, previous /*null when async*/ ) {

var gr = new GlideRecord("incident");
gr.addEncodedQuery('closed_at!=NULL^business_duration=NULL');
gr.autoSysFields(false); // so that the records don't have system updates
gr.query();
while (gr.next()) {
var gsBusiness = new GlideSchedule('sys_id of your schedule'); //update sys_id of your schedule
// Get duration based on schedule
gr.business_duration = gsBusiness.duration(gr.opened_at.getGlideObject(), gr.closed_at.getGlideObject());
gr.setWorkflow(false);
gr.update();
}

})(current, previous);

 

5 REPLIES 5

Prana Krushna
Giga Guru

You can check the link, It may can help
https://developer.servicenow.com/dev.do#!/reference/api/washingtondc/server/no-namespace/c_GlideSche...

PranaKrushna_0-1735128071952.png

Mark this Helpful if it works.

No this doesn't answer my question of why the script doesn't calculate properly when a ticket is created outside of the schedule time or why I'm getting 0 seconds for some of these calculations. 

Have you tried as per documentation i have added and the screenshot for start and end
for this

PranaKrushna_0-1735535504323.png

Please provide that as start and end as per screenshot, put the date time in gsBusiness.duration(new GlideDateTime(gr.opened_at), new GlideDateTime(gr.closed_at))function.

I've already done that if you look at the code I pasted originally.