Catalog variable Date/Time validation – 2 working days (Sun–Thu) with hour‑based lead time

Sirri
Tera Guru

Hi Community,

I’m working on a Service Catalog Date/Time variable validation and would like confirmation or suggestions for improvement.

Variable Details

  • Variable name: time_to_send_out_comms
  • Variable type: Date/Time
  • Used on: Service Request (SR)

    Business Requirement

    1. Past Date/Time Validation

    • The selected date/time must not be in the past

    If user selects a past date/time, show the error message:

    “The time chosen to send out the communication occurs in the past, kindly select a future date/time to proceed.”

    2. Minimum Lead Time – 2 Working Days (Hour‑Based)

    • The selected date/time must be at least 2 working days after the current date/time
    • Working days: Sunday to Thursday
      (Friday and Saturday are non‑working days)
    • Lead time must be hour‑based, not just date‑based

    Example

    Current date/time:

    13/May/2026 13:05:00 (Wednesday)

    Valid selections

    • 17/May/2026 13:06:00
    • 17/May/2026 15:00:00
    • Any date/time after the same HH:mm:ss on the 2nd working day

    Invalid selections

    • 17/May/2026 13:04:00
    • Any date/time before 17/May/2026 13:05:00

    If user does not allow 2 working days lead time, show the error message:

    “Please allow two working days lead time for approvals and content review.”
    Please provide with exact code so it will helpful to me.

    Thank you

     





13 REPLIES 13

@Vishal Jaswal ,

Thank you for your response. I have given same code which you have given :

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

    //Type appropriate comment here, and begin script below

    var pastMsg = "The time chosen to send out the communication occurs in the past, kindly select a future date/time to proceed.";
    var leadMsg = "Please allow two working days lead time for approvals and content review.";

    var parts = newValue.split(' ');
    var d = parts[0].split('-');
    var t = parts[1].split(':');

    var selected = new Date(
        parseInt(d[0], 10),
        parseInt(d[1], 10) - 1,
        parseInt(d[2], 10),
        parseInt(t[0], 10),
        parseInt(t[1], 10),
        parseInt(t[2], 10)
    );

    var now = new Date();

    // Past date validation
    if (selected.getTime() < now.getTime()) {
        alert(pastMsg);
        g_form.setValue('time_to_send_out_comms', '');
        return;
    }

    // Lead time calculation (2 working days, excluding Fri & Sat)
    var minDate = new Date(now.getTime());
    var workingDaysAdded = 0;

    while (workingDaysAdded < 2) {
        minDate.setDate(minDate.getDate() + 1);
        var dayOfWeek = minDate.getDay();

        if (dayOfWeek !== 5 && dayOfWeek !== 6) {
            workingDaysAdded++;
        }
    }

    // Lead time validation
    if (selected.getTime() < minDate.getTime()) {
        alert(leadMsg);
        g_form.setValue('time_to_send_out_comms', '');
    }

}

Please check If I'm missing anything in onchange client script i have selected variable as 
time_to_send_out_comms but still it's not working for me for reference please check below snips.
Sirri_0-1778744434260.pngSirri_1-1778744523828.png

Nothing is coming when I selected past date or withing 2 working days. I'm not sure is this active are not Please can you provide the xml of that script and provide the name of that script so after importing i can verify. If I'm missing anything.



@Sirri 

I will suggest to use GlideAjax and schedule for validating business days

if customer in future says to follow schedule to validate business day the script needs to be changed again

did you check the link I shared which has approach using GlideAjax and Schedule?

Variable Date no less than 5 business days 

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

Regards,
Ankur
Certified Technical Architect  ||  10x ServiceNow MVP  ||  ServiceNow Community Leader

@Ankur Bawiskar ,
Thank you for your response.
But there is no schedule OOB which says Sunday to Thursday in that case it is very difficult right? Please help me with exact code which as per my requirement.




@Sirri 

if customer is using that schedule then it should be there in the instance

They must be using schedule for their Incident SLAs etc why not use that only?

Regards,
Ankur
Certified Technical Architect  ||  10x ServiceNow MVP  ||  ServiceNow Community Leader

@Ankur Bawiskar ,

I have reviewed the approach you provided; however, it is still not working as expected. After applying my customizations, I am still facing issues. whatever i select the date every time it is showcasing the alert this not expected. Could you please review and help identify what might be missing?

For your reference, I am sharing the updated implementation below:

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

 

    var ga = new GlideAjax('CommsDateValidator');
    ga.addParam('sysparm_name', "getInfo");
    ga.addParam('sysparm_date', g_form.getValue('time_to_send_out_comms'));
    ga.getXMLAnswer(function(answer) {
        if (answer == 'true') {
            alert('Please select a date after 2 business days');
            g_form.clearValue('time_to_send_out_comms');
        }
    });
}
Script Include
Name: CommsDateValidator
Script:
var CommsDateValidator = Class.create();
CommsDateValidator.prototype = Object.extendsObject(AbstractAjaxProcessor, {

 

    getInfo: function() {

 

        var dateSelected = this.getParameter('sysparm_date');
        var nowDateTime = new GlideDateTime();

 

        var days = 2;
        var dur = new GlideDuration(days * 43200 * 1000);

 

        // Schedule: 8x8 weekday schedule (Sun–Thu, excluding holidays)
        var schedule = new GlideSchedule('18391defdb044210707e0474f39619fa');

 

        var finalTime = schedule.add(nowDateTime, dur, '');
        var updatedGdt = new GlideDateTime(dateSelected);
        var finalTimeGdt = new GlideDateTime(finalTime);

 

        if (updatedGdt < finalTimeGdt) {
            return 'true';
        }
        return 'false';
    },

 

    type: 'CommsDateValidator'
});

I have also attached the schedule configuration snippets for your review. Kindly let me know if I have missed anything or if further adjustments are required.
I have added the sys id of the schedule (RX IT Working Hours) not schedule entry (Sunday-Thursday 7:30-6:30) I hope that is correct right?
Sirri_0-1778834597767.pngSirri_1-1778834630439.png


Thank you