The CreatorCon Call for Content is officially open! Get started here.

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

Thanks @Upsilon , I think getDatefromFormat did the trick here!

Servicenow34
Tera Guru

Have you checked if type values are satisfying the if condition ?

 

Sandeep Rajput
Tera Patron
Tera Patron

Use the following script and let me know if it works for you.

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 = 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;
        }
    }
}

 

Also make sure that you have the highlighted fields available on the form in order to make this script work.

 

Screenshot 2023-04-11 at 5.43.23 PM.png

In your script, you are fetching the value of change type from 

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

 

Where as it should be

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

Ankur Bawiskar
Tera Patron
Tera Patron

@JordyZ 

Is sys_created_on field on form? if not then your script won't work

If yes this will work

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(g_form.getValue('start_date')).getTime();
	var opened = new Date(g_form.getValue('end_date')).getTime();

	if (type == 'normal' || type == 'standard' || type == 'emergency') {
		if (plannedStart < opened) {
			alert('Planned start date cannot be before opened date');
			return false;
		}
	}
}

If it's not on form then use display BR on change_request table and pass the sys_created_on via g_scratchpad variable

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(g_form.getValue('start_date')).getTime();
	var opened = new Date(g_scratchpad.createdTime).getTime(); // pass the g_scratchpad from display BR

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

		if (plannedStart < opened) {

			alert('Planned start date cannot be before opened date');
			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

@JordyZ 

Thank you for marking my response as helpful.

As per new community feature you can mark multiple responses as correct.

If my response helped please mark it correct so that it benefits future readers.

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