- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-30-2024 05:15 PM
Hi everyone,
I’m working on a ServiceNow Catalog item and need help with configuring the behaviour of a date field (Required by Date). The goal is to prepopulate the field with a specific date (current date + 2 days) when the form loads, while still allowing users to modify it to any date in the future.
The Scenario:
- Catalog Item Example:
- The Required by Date field should:
- Automatically populate with the current date + 2 days when the form loads.
- Allow users to change the date, but only to a future date (i.e., today’s date + 1 day or later).
- Not be a mandatory field—users can leave it blank if they want.
- The Required by Date field should:
The Problem:
I tried creating a client script to handle this behaviour, but it doesn’t work as expected. Here’s what’s happening:
- The Required by Date field is not prepopulated as intended.
- Users are able to select past dates, which defeats the purpose of the validation.
Additionally, when I reload the customer portal, I see a red error message stating:
"There is a JavaScript error in your browser console."
I’m new to ServiceNow and not entirely sure how to debug or check the error in more detail. If anyone can guide me on how to debug this issue, I’d really appreciate it!
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-02-2025 07:10 PM
you need to have onChange validation on that date variable to stop users from selecting past date
something like this
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var today = new Date().getTime();
var selectedDate = new Date(newValue).getTime();
if (today > selectedDate) {
alert('You cannot select past date');
g_form.clearValue('selected_date');
g_form.setMandatory('selected_date');
}
}
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-02-2025 07:25 PM
Hi @JGuerrero0323 ,
There are already a lot of solutions out there for this requirement.
No Code date validations through (Catalog) UI Policies -> the no code approach
How to restrict past date selection. -> with code
for Automatically populate with the current date + 2 days when the form loads. put this code in the default value filed of the date variable
javascript: (function(){
var gd = new GlideDate();
gd.addDays(2);
return gd.getDisplayValue()
})();
Please mark my answer as helpful/correct if it resolves your query.
Regards,
Chaitanya
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-02-2025 02:14 PM
Hi Ankur,
Thank you for your suggestion—you’re absolutely right! The default value in the variable does prepopulate the field, but it doesn’t restrict users from selecting a previous date.
Your help is much appreciated, and your idea of setting the current date + 2 days in the variable is a great starting point for me. Thanks again for your support!
Best regards, Juan
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-02-2025 07:10 PM
you need to have onChange validation on that date variable to stop users from selecting past date
something like this
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var today = new Date().getTime();
var selectedDate = new Date(newValue).getTime();
if (today > selectedDate) {
alert('You cannot select past date');
g_form.clearValue('selected_date');
g_form.setMandatory('selected_date');
}
}
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-02-2025 07:25 PM
Hi @JGuerrero0323 ,
There are already a lot of solutions out there for this requirement.
No Code date validations through (Catalog) UI Policies -> the no code approach
How to restrict past date selection. -> with code
for Automatically populate with the current date + 2 days when the form loads. put this code in the default value filed of the date variable
javascript: (function(){
var gd = new GlideDate();
gd.addDays(2);
return gd.getDisplayValue()
})();
Please mark my answer as helpful/correct if it resolves your query.
Regards,
Chaitanya