If start time of Change is in business hours, a new mandatory field needs to display.

StewartFletcher
Tera Expert

We have a requirement on Change Requests, that a new mandatory field be visible to users if they select a state time for their Change that is within business hours. I know how to set this to appear Mon-Fri, but I need to be able to also get it to only show if they select the start time as being between 08:00 and 18:00 Mon-Fri. How would I achieve this with a Client Script please?

22 REPLIES 22

We're almost there, thank you @Mark Manders - however, slight issue.

 

If it runs over 14hrs, then it triggers. Great, but if it runs say 07:00 to 18:30 on a single day, that's less than 14hrs and therefore doesn't trigger. If I change the 14 to be 10, then it does. However, that then triggers if it's overnight for say 19:00 to 06:00 - it's over 10hrs, but fully out of business hours. So I need to figure out that issue now. Needs to trigger for anything up to 10hrs over business hours, but over 14hrs out of business hours...

 

Sorry it's turning out far more complicated than initially suspected!

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }

    var startTime = g_form.getValue('start_date');
    var endTime = g_form.getValue('end_date');
    
    var showField = spansBusinessHours(startTime, endTime);

    g_form.setMandatory('u_workinghours', showField);
    g_form.setVisible('u_workinghours', showField);
}

function spansBusinessHours(start, end) {
    var startTime = new Date(start);
    var endTime = new Date(end);

    // If the end time is before the start time, adjust it to the next day
    if (endTime < startTime) {
        endTime.setDate(endTime.getDate() + 1);
    }

    // Check each hour from start to end to see if it's within business hours
    while (startTime < endTime) {
        var dayOfWeek = startTime.getDay();
        var hour = startTime.getHours();
        // Check if the current hour is within business hours
        if (dayOfWeek > 0 && dayOfWeek < 6 && hour >= 8 && hour < 18) {
            return true;
        }
        // Move to the next hour
        startTime.setHours(startTime.getHours() + 1);
    }
    return false;
}

Please mark any helpful or correct solutions as such. That helps others find their solutions.
Mark

Doesn't work I'm afraid - even trying start of 19:00 and ending at 00:00 for the next day, 5hrs, it flags up that it's in business hours, despite the fact it isn't.