The date TO should be no later then the end of the current year.

Fotina Galieb
Tera Contributor
Hello Team!
 
 
The date TO should be no later then the end of the current year. If customer provides date after 31.12 of the current year, present an ERROR message "Date should not exceed last day of the current year." I need to use the onChange client script.
 
 

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!

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@Fotina Galieb 

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.

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

View solution in original post

3 REPLIES 3

Siddhesh Jadhav
Kilo Sage

Hello @Fotina Galieb ,

 

Solution:

Here’s how you can achieve the functionality using an onChange client script:

  1. 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.
  2. 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.
  3. 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.

SiddheshJadhav_0-1737299951554.png

 


If this answer helps you solve your query, please mark it as accepted and helpful.

Best Regards,
Siddhesh Jadhav

Ankur Bawiskar
Tera Patron
Tera Patron

@Fotina Galieb 

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.

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

Thank you @Ankur Bawiskar it worked!