Get current date on a Catalog Client Script, and compare it with Variable date field?

xhensilahad
Tera Expert

Hi,

What is the easiest way to achieve the following in a Catalog Client Script

1) Get current date time

2) Compare it with Date Input Variable

 

If Input Variable before today's date, then Show a field message.

 

Thank you in advance for all your answers.

1 ACCEPTED SOLUTION

Looks like its expected. 

Data validation showFieldMsg not working in Service Portal - Known Error (servicenow.com)

 

The workaround is to use clearValue before showFieldMsg if you want the message to persist.

 

 

-Anurag

View solution in original post

9 REPLIES 9

Can you show screenshot of everything 

-Anurag

xhensilahad_0-1729777414945.pngxhensilahad_1-1729777441443.png

 

Looks like its expected. 

Data validation showFieldMsg not working in Service Portal - Known Error (servicenow.com)

 

The workaround is to use clearValue before showFieldMsg if you want the message to persist.

 

 

-Anurag

Hi Anurag,

The requirement has now changed to allow today's date to be selected, but not before current time.

The UI Policy in this case does not work.

Any suggestions on how to achieve this in a Catalog Client Script?

Thank you

Ashish Parab
Mega Sage

Hello @xhensilahad ,

 

Below is a onSubmit catalog client script:

function onSubmit() {
    // Get current date and time
    var currentDate = new Date();

    // Get the input variable value (replace 'date' with the actual variable name)
    var inputDate = g_form.getValue('date');

    // Convert input date string to a Date object
    var inputDateObj = new Date(inputDate);

    // Compare the input date with the current date
    if (inputDateObj < currentDate) {
        // Show field message if the input date is before today
        g_form.addErrorMessage('The selected date cannot be in the past.');
		g_form.clearValue('date');
        return false; // Prevent form submission
    }

    return true; // Allow form submission
}

 

Please mark this as "correct" and "helpful" if you feel this answer helped you in anyway.

 

Thanks and Regards,

Ashish