- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-19-2025 06:57 AM
I know I need to use the onChange client script. I found examples withiin several days or within year but how to write exactly no longer than last day of the current year?Can someone please help me with it?
Thank you in advance!
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-19-2025 08:10 AM
something like this in onChange client script on that date field
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue === '') {
return; // Exit if the form is loading or the field is empty
}
var dateFieldName = 'u_my_date';
g_form.hideFieldMsg(dateFieldName);
// Get the current year
var currentYear = new Date().getFullYear();
// Define the last date of the current year (December 31st)
var lastDayOfYear = new Date(currentYear, 11, 31); // Months are 0-indexed
// Convert the new value (user input) into a Date object
var providedDate = new Date(getDateFromFormat(g_form.getValue(dateFieldName), g_user_date_format));
// Check if the provided date exceeds the last day of the current year
if (providedDate.getTime() > lastDayOfYear.getTime()) {
// Show error message
g_form.showFieldMsg(dateFieldName, "Date should not exceed last day of the current year.", "error");
// Clear the invalid date
g_form.clearValue(dateFieldName);
}
}
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-19-2025 07:04 AM - edited 01-19-2025 07:19 AM
Hello @Fotina Galieb ,
Solution:
Here’s how you can achieve the functionality using an onChange client script:
Understanding the Requirement:
- The "TO" date provided by the user should not exceed December 31st of the current year.
- If it does, an error message needs to be displayed.
Logic for the Script:
- Get the current date and determine the last day of the current year.
- Compare the provided date with December 31st of the current year.
- If the provided date exceeds this limit, display an error message and clear the field.
Steps to Implement:
- Navigate to the table where the "TO" date field exists.
- Create an onChange Client Script for the "TO" date field.
- Add the following logic in the script:
Example onChange Client Script:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue === '') {
return; // Exit if the form is loading or the field is empty
}
// Get the current year
var currentYear = new Date().getFullYear();
// Define the last date of the current year (December 31st)
var lastDayOfYear = new Date(currentYear, 11, 31); // Months are 0-indexed
// Convert the new value (user input) into a Date object
var providedDate = new Date(newValue);
// Check if the provided date exceeds the last day of the current year
if (providedDate > lastDayOfYear) {
// Show error message
g_form.showErrorBox(control, "Date should not exceed last day of the current year.");
// Clear the invalid date
g_form.setValue(control, '');
} else {
g_form.hideErrorBox(control); // Hide error if input is valid
}
}
Debugging and Validation:
- Test the client script by entering different dates in the "TO" field.
- Ensure that valid dates are accepted and invalid dates show the error message.
If this answer helps you solve your query, please mark it as accepted and helpful.
Best Regards,
Siddhesh Jadhav
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-19-2025 08:10 AM
something like this in onChange client script on that date field
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue === '') {
return; // Exit if the form is loading or the field is empty
}
var dateFieldName = 'u_my_date';
g_form.hideFieldMsg(dateFieldName);
// Get the current year
var currentYear = new Date().getFullYear();
// Define the last date of the current year (December 31st)
var lastDayOfYear = new Date(currentYear, 11, 31); // Months are 0-indexed
// Convert the new value (user input) into a Date object
var providedDate = new Date(getDateFromFormat(g_form.getValue(dateFieldName), g_user_date_format));
// Check if the provided date exceeds the last day of the current year
if (providedDate.getTime() > lastDayOfYear.getTime()) {
// Show error message
g_form.showFieldMsg(dateFieldName, "Date should not exceed last day of the current year.", "error");
// Clear the invalid date
g_form.clearValue(dateFieldName);
}
}
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-19-2025 01:55 PM
Thank you @Ankur Bawiskar it worked!