- 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
12-30-2024 06:34 PM
Hi @JGuerrero0323 ,
you need to write two client script one for onload and second for Onchange.
use below script .
onload
function onLoad() {
// Create GlideDateTime object for current time
var gdtNow = new GlideDateTime();
// Add 2 days to the current date
gdtNow.addDays(2);
// Get the date in the format required by ServiceNow (YYYY-MM-DD)
var requiredByDate = gdtNow.getDate().toString();
// Set the value of the 'Required by Date' field
g_form.setValue('required_by_date', requiredByDate);
}
Onchange for required by date.
function onChange(control, oldValue, newValue, isLoading) {
// Avoid processing if the form is still loading
if (isLoading) return;
// Get the current date and the selected date
var gdtNow = new GlideDateTime();
var gdtSelected = new GlideDateTime(newValue);
// Add 1 day to the current date to create the minimum valid date
gdtNow.addDays(1);
// Compare the selected date with the minimum allowed date
if (gdtSelected.before(gdtNow)) {
// If selected date is before today + 1 day, reset to the minimum date
g_form.setValue('required_by_date', gdtNow.getDate().toString());
// Display an error message for the user
g_form.showFieldMsg('required_by_date', 'Please select a future date (today + 1 day or later).', 'error');
}
}
Accept and mark helpful if my response helped you
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-02-2025 02:21 PM
Hi Runjay,
Thank you so much for sharing this approach—I really appreciate your help! The scripts you provided make a lot of sense, and I think they’re a great starting point for me.
That said, I couldn’t quite get the desired output when I implemented them. I’m wondering if I might be missing something—either in the script itself or perhaps a setting that needs to be adjusted for it to work correctly.
Do you have any tips on how to troubleshoot in situations like this? For instance, how do you usually test if the issue lies in the code or in the configuration?
Thanks again for your support and guidance!
Best regards, Juan
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-30-2024 06:40 PM
so it should default to current date+2 days
you can set default value in that variable like this and it will auto populate when form loads.
No need of client script or script include
User can still edit that
javascript: var gdt = new GlideDateTime(date);
gdt.addDaysUTC(2);
gdt.getDate();
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-01-2025 07:03 PM
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.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader