Restrict the 'Date' variable in a catalog item to only allow only the 3 month clickable

ShubhiSharm
Tera Contributor

How to restrict the 'Date' variable in a catalog item to only allow only the 3 month clickable

2 ACCEPTED SOLUTIONS

Ankur Bawiskar
Tera Patron
Tera Patron

@ShubhiSharm 

2 approaches

1) you can use catalog UI policy without scripting

AnkurBawiskar_0-1747755756215.png

AnkurBawiskar_1-1747755793966.png

 

 

2) If you want you can use onChange catalog client script as well

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    var selectedDate = new Date(newValue);
    var today = new Date();
    // Set time to 00:00:00 for accurate comparison
    today.setHours(0,0,0,0);

    // Calculate max date (3 months from today)
    var maxDate = new Date(today);
    maxDate.setMonth(maxDate.getMonth() + 3);

    if (selectedDate < today || selectedDate > maxDate) {
        g_form.addErrorMessage('Please select a date within the next 3 months.');
        g_form.clearValue('your_date_variable_name');
    }
}

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

View solution in original post

@ShubhiSharm 

Hope you are doing good.

Did my reply answer your question?

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

View solution in original post

3 REPLIES 3

Brian Lancaster
Tera Sage

Take a look at this. It may not have 100% what you are looking for but it will have a script include which is what you will need. You may just need to add another function to it.

https://www.servicenow.com/community/in-other-news/auto-populating-and-validating-date-fields/ba-p/2...

Ankur Bawiskar
Tera Patron
Tera Patron

@ShubhiSharm 

2 approaches

1) you can use catalog UI policy without scripting

AnkurBawiskar_0-1747755756215.png

AnkurBawiskar_1-1747755793966.png

 

 

2) If you want you can use onChange catalog client script as well

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    var selectedDate = new Date(newValue);
    var today = new Date();
    // Set time to 00:00:00 for accurate comparison
    today.setHours(0,0,0,0);

    // Calculate max date (3 months from today)
    var maxDate = new Date(today);
    maxDate.setMonth(maxDate.getMonth() + 3);

    if (selectedDate < today || selectedDate > maxDate) {
        g_form.addErrorMessage('Please select a date within the next 3 months.');
        g_form.clearValue('your_date_variable_name');
    }
}

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

@ShubhiSharm 

Hope you are doing good.

Did my reply answer your question?

If my response helped please mark it correct and close the thread so that it benefits future readers.

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