- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-23-2024 10:38 PM
Hi Team,
We have a catalog client script(Onsubmit) for two variables (start date and End date) - For example we are selecting 25-12-2024 in start date and 10-01-2025 in End date. As per our script it should allow to take the value in end date. But it is throwing an error. Attached screenshot for your reference.
While we are taking 25-12-2024 in start date and 26-01-2025 in end date - It is taking working as expected attached screenshots for your reference. Here we have noticed that calculating date number only not month and year.
How to fix this bug - Below is the client script(Onsubmit):-
-------------------------------------------------
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-24-2024 12:33 AM
is it working fine in native?
try this
function onSubmit() {
//Type appropriate comment here, and begin script below
g_form.hideFieldMsg('start_date');
g_form.hideFieldMsg('end_date');
if (g_form.getValue('start_date') != '' && g_form.getValue('end_date')) {
var start = new Date(getDateFromFormat(g_form.getValue('start_date'), g_user_date_format));
var end = new Date(getDateFromFormat(g_form.getValue('end_date'), g_user_date_format));
if (end.getTime() < start.getTime()) {
var message = 'Please give valid start and end dates';
g_form.showFieldMsg('start_date', message, 'error');
g_form.showFieldMsg('end_date', message, 'error');
return false;
}
}
}
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-06-2025 06:54 AM
you can have onChange on each of those 2 date variables/field and inform user to select only future date
something like this
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading) {
return;
}
var today = new Date().getTime();
var selectedDate = new Date(newValue).getTime();
if (today > selectedDate) {
alert('You cannot select past date');
g_form.clear('start_date');
g_form.setMandatory('start_date', true);
}
}
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
12-23-2024 11:44 PM
Same issue we are selecting 25-12-2024 in start date and 10-01-2025 in End date. As per our script it should allow to take the value in end date - While we are taking 25-12-2024 in start date and 26-01-2025 in end date - It is taking working as expected. Here we have noticed that calculating date number only not month and year.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-23-2024 11:04 PM - edited 12-23-2024 11:04 PM
Hi @thaduri sai ,
try below code.
function onSubmit() {
var requestedEndDate = g_form.getValue('end_date');
var requestedStartDate = g_form.getValue('start_date');
var format = g_user_date_time_format;
var startDateMs = getDateFromFormat(requestedStartDate, format);
var endDateMs = getDateFromFormat(requestedEndDate, format);
if (startDateMs > endDateMs) {
g_form.showFieldMsg('end_date', 'End date should be later than start date.', 'error');
return false;
}
}
-------------------------------------------------------------------------
If you found my response helpful, please consider selecting "Accept as Solution" and marking it as "Helpful." This not only supports me but also benefits the community.
Regards
Runjay Patel - ServiceNow Solution Architect
YouTube: https://www.youtube.com/@RunjayP
LinkedIn: https://www.linkedin.com/in/runjay
-------------------------------------------------------------------------
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-23-2024 11:44 PM
Hi @Runjay Patel ,
Above script is not working like start date 25-12-2024 and end date 21-12-2024 if am selecting past end date am able to submit. Kindly please let me know?
Thanks,
Saikrishna
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-24-2024 01:19 AM
Hi @thaduri sai ,
try below code, i have tested it, working fine.
var requestedEndDate = g_form.getValue('end_date');
var requestedStartDate = g_form.getValue('start_date');
var startDate = new Date(requestedStartDate.split('-').reverse().join('-'));
var endDate = new Date(requestedEndDate.split('-').reverse().join('-'));
// Compare the dates
if (startDate > endDate) {
g_form.showFieldMsg('end_date', 'End date should be later than start date.', 'error');
return false; // Prevent further processing
}
-------------------------------------------------------------------------
If you found my response helpful, please consider selecting "Accept as Solution" and marking it as "Helpful." This not only supports me but also benefits the community.
Regards
Runjay Patel - ServiceNow Solution Architect
YouTube: https://www.youtube.com/@RunjayP
LinkedIn: https://www.linkedin.com/in/runjay
-------------------------------------------------------------------------
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-24-2024 12:01 AM
Hi @thaduri sai you can achieve this without script also using ui policy check below link also done by script i will share both link below
https://www.servicenow.com/community/itsm-forum/end-date-greater-than-start-date/m-p/2493307
if my answer helps you mark helpful and accept solution