Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Restrict Incident Date Selection ONE MONTH AGO

may13
Tera Contributor

I have created a record producer with a variable called Incident Date, and I need to restrict users from selecting a date that is more than one month old.

 
For example - if today’s date is 07-Dec-2025 and I select 06-Nov-2025 or any date before that, an error message should pop up.


3 REPLIES 3

Ankur Bawiskar
Tera Patron
Tera Patron

@may13 

you can write onChange client script and validate like this

Note: I assume 1 month is 30 days

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

    g_form.hideFieldMsg('fielName');

    var selectedDate = new Date(newValue);

    var todayDate = new Date(); // Now
    todayDate.setDate(todayDate.getDate() + 30);

    if (selectedDate.getTime() < todayDate.getTime()) {
        g_form.showFieldMsg('fieldName', 'Please select valid date', 'error');
    }

}

💡 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  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

@may13 

Thank you for marking my response as helpful.

💡 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  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Chaitanya ILCR
Mega Patron

HI @may13 ,

 

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

    var selectedDate = new Date(getDateFromFormat(newValue, g_user_date_format));

    var currentDate = new Date();
    currentDate.setMonth(currentDate.getMonth() - 1);
    currentDate.setDate(currentDate.getDate() - 1);
    if (selectedDate.valueOf() >= currentDate.valueOf()) {
        alert('please select on month old date');
		//write your additional code here to clear the value or whatever d action dat you want to take
    }

}

create a onChange catalog client script on the record producer on the incident date variable with above script

 

Please mark my answer as helpful/correct if it resolves your query.

Regards,
Chaitanya