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

Thank you, Ankur this is working, but I need to allow todays and future, how can I do that.

Runjay Patel
Giga Sage

Hi @sattimsetti ,

 

Use below script, i have tested it in my PDI, working fine.

function onSubmit() {

    var changeType = g_form.getValue('type');
    var requestedStartDate = g_form.getValue('start_date');

    var todayDate = new Date();
    var format = g_user_date_time_format;


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

        var startDateMs = getDateFromFormat(requestedStartDate, format);
        var todatDateMs = getDateFromFormat(todayDate, format);

        if (startDateMs > todatDateMs) {
            g_form.showFieldMsg('start_date', 'The requested start date cannot be in the past 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

-------------------------------------------------------------------------

Hello Runjay Patel,

 

Thank you for response !!

 

When I select past date and future also getting an error. 

 

Thanks,

Raju