Planned start/end date cannot be before the created date

JordyZ
Mega Sage

Hi,

 

On the change form, I'd like an alert for when a date is selected for the planned start/end date that is before the created date of the change. I have an onChange client script but it's not giving me an alert.

 

This is my current onChange client script for planned start date (same one for planned end date):

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {


    if (isLoading || newValue === '') {


        return;


    }


    //Type appropriate comment here, and begin script below


    var type = g_form.getValue('change_type'); 


    var plannedStart = g_form.getValue('start_date');

    var opened = g_form.getValue('sys_created_on');


    if (type == 'normal' || type == 'standard' || type == 'emergency') { 

        if (plannedStart < opened) {


            alert('Planned start date cannot be before opened date'); 


            return false;


        }


    }


}

 

 What's wrong with it? Thanks in advance!

1 ACCEPTED SOLUTION

Upsilon
Giga Guru

Hello, 

Make sure that created field is displayed in the form and try this  script

Nota: in my instance change type field is "type" and not "change_type"

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {


if (isLoading || newValue === '') {


return;


}


//Type appropriate comment here, and begin script below


var type = g_form.getValue('type');

var plannedStart = new Date(getDateFromFormat(newValue, g_user_date_time_format));

var opened = new Date(getDateFromFormat(g_form.getValue('sys_created_on'), g_user_date_time_format));

if (type == 'normal' || type == 'standard' || type == 'emergency') {

if (plannedStart < opened) {


alert('Planned start date cannot be before opened date');


return false;


}


}


}

 

 

View solution in original post

9 REPLIES 9

AndersBGS
Tera Patron
Tera Patron

Hi @JordyZ ,

 

Please look into the community post from 2017, whit same requirement as yours: https://www.servicenow.com/community/itsm-forum/planned-start-date-should-be-after-created-date-for-...

 

If my answer has helped with your question, please mark my answer as accepted solution and give a thumb up.

 

best regards

Anders

If my answer has helped with your question, please mark my answer as the accepted solution and give a thumbs up.

Best regards
Anders

Rising star 2024
MVP 2025
linkedIn: https://www.linkedin.com/in/andersskovbjerg/

Hi @AndersBGS thanks for replying!

 

My client script is actually based on the one provided in that 2017 community post, unfortunately it doesn't work for me. 

Hi @JordyZ ,

 

Do you receive any errors? Have you run it through a debugger? 

 

If my answer has helped with your question, please mark my answer as accepted solution and give a thumb up.

 

best regards

Anders

If my answer has helped with your question, please mark my answer as the accepted solution and give a thumbs up.

Best regards
Anders

Rising star 2024
MVP 2025
linkedIn: https://www.linkedin.com/in/andersskovbjerg/

Upsilon
Giga Guru

Hello, 

Make sure that created field is displayed in the form and try this  script

Nota: in my instance change type field is "type" and not "change_type"

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {


if (isLoading || newValue === '') {


return;


}


//Type appropriate comment here, and begin script below


var type = g_form.getValue('type');

var plannedStart = new Date(getDateFromFormat(newValue, g_user_date_time_format));

var opened = new Date(getDateFromFormat(g_form.getValue('sys_created_on'), g_user_date_time_format));

if (type == 'normal' || type == 'standard' || type == 'emergency') {

if (plannedStart < opened) {


alert('Planned start date cannot be before opened date');


return false;


}


}


}