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

This worked for me. I created a new schedule in "cmn_schedule" and a schedule entry for it. 

 

onBefore Business Rule

On Insert or Update

 

if (current.state == 3 || current.state == 4 || current.state == 7) {
    current.active = false;

    if (current.closed_by.nil())
        current.closed_by = gs.getUserID();

    if (current.closed_at.nil()) {
        current.closed_at = gs.nowDateTime();

        // 'Business Duration 8-5 Weekdays' Schedule
        var schedule = new GlideSchedule("73342611975cee10a15eb5400153af85");

        var duration = schedule.duration(current.opened_at.getGlideObject(), current.closed_at.getGlideObject());

        current.business_duration = duration;

        current.calendar_duration = gs.dateDiff(current.opened_at.getDisplayValue(), current.closed_at.getDisplayValue(), false);

    }
}