Welcome to Community Week 2025! Join us to learn, connect, and be recognized as we celebrate the spirit of Community and the power of AI. Get the details  

Restrict the submission of Normal and Standard change requests with a past date.

sattimsetti
Tera Contributor

Hello Team,

 

We have requirement "Restrict the submission of Normal and Standard change requests with a past date"

 

I have created below script but it's not working. Please help me on this

 

function onSubmit() {

    var changeType = g_form.getValue('type');
    var requestedStartDate = g_form.getValue('start_date');
    var currentDate = new Date();
    var format = g_user_date_time_format;
    var startDateMs = getDateFromFormat(requestedStartDate, format);
    var CurrentDateMs = getDateFromFormat(currentDate, format);
    if ((changeType == 'Normal' || changeType == 'Standard') && startDateMs) {
     
        if (startDateMs < CurrentDateMs) {

            g_form.showFieldMsg('start_date', 'The requested start date cannot be in the past.', 'error');
            return false;
        }
    }


    return true;
}
7 REPLIES 7

Ankur Bawiskar
Tera Patron
Tera Patron

@sattimsetti 

use this

function onSubmit() {

    var changeType = g_form.getValue('type');
    var requestedStartDate = g_form.getValue('start_date');
    if ((changeType == 'Normal' || changeType == 'Standard') && requestedStartDate) {
        var today = new Date().getTime();
        var selectedDate = new Date(requestedStartDate).getTime();
        if (today > selectedDate) {
            g_form.showFieldMsg('start_date', 'The requested start date cannot be in the past.', 'error');
            return false;
        }
    }
}

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

Hello Ankur Bawiskar,

 

Thank you for Response!!

 

It's not working still allow me to submit the past date.

 

Regards,

Raju

Ankur's answer will work for you; just replace Normal with normal and Standard with standard. If you want an error message instead of field message, you can use 

g_form.addErrorMessage('The requested start date cannot be in the past.')
 

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

 

Thanks and Regards,

Ashish

@sattimsetti 

I hope you are using the correct choice values for normal and standard

    if ((changeType == 'normal' || changeType == 'standard') && requestedStartDate) {

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