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

Mark Manders
Mega Patron

Something like this should get you started

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

    // Parse the new value to extract the day and time
    var startDateTime = new GlideDateTime(newValue);
    var dayOfWeek = startDateTime.getDayOfWeek();
    var hours = startDateTime.getGlideObject().getHourOfDayLocalTime();
    var minutes = startDateTime.getGlideObject().getMinuteOfHourLocalTime();

    // Convert hours and minutes to a single number for comparison
    var time = hours + (minutes / 60);

    // Check if the selected time is within business hours and on a weekday
    var withinBusinessHours = time >= 8 && time <= 18;
    var isWeekday = dayOfWeek >= 2 && dayOfWeek <= 6;

    // Show or hide the field based on the conditions
    g_form.setVisible('YOUR FIELD', withinBusinessHours && isWeekday);
}

 


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

When entering a date/time into the Planned State Date field (the one that needs to be referenced) get this error - onChange script error: ReferenceError: GlideDateTime is not defined function () { [native code] }

 

The u_workinghours field that needs hiding, is visible at all times.

Pushpanjali
Tera Contributor

on change of client script you can achieve,

also using Business rule

Use Glide Record respective table name, Add condition for state time from 08:00 and 18:00 Mon-Fri

based on that set new mandatory field visibility true/false.

 

Mark my answer helpful ...if it is helpful for your query.

 

Regards,

Pushpanjali

Maddysunil
Kilo Sage

@StewartFletcher 

I think you can write on change client script on start time, below is the sample code:

 

    var startTime = g_form.getValue('start_time_field'); // Replace 'start_time_field' with the actual ID or name of the start time field
    var newMandatoryField = g_form.getControl('new_mandatory_field'); // Replace 'new_mandatory_field' with the actual ID or name of the new mandatory field

    // Check if the selected start time falls within business hours (Monday to Friday, 08:00 to 18:00)
    var startDateTime = new Date(startTime);
    var dayOfWeek = startDateTime.getDay(); // 0 (Sunday) to 6 (Saturday)
    var hours = startDateTime.getHours();

    if (dayOfWeek >= 1 && dayOfWeek <= 5 && hours >= 8 && hours < 18) {
        // Start time falls within business hours, make the new mandatory field visible
        g_form.setMandatory(newMandatoryField, true);
        g_form.setDisplay(newMandatoryField, true);
    } else {
        // Start time falls outside business hours, hide the new mandatory field
        g_form.setMandatory(newMandatoryField, false);
        g_form.setDisplay(newMandatoryField, false);
    }

 

Please Mark Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.

 

Thanks