Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Planned start date cannot be earlier than current date

Suresh36
Tera Expert

I have one requirement, Planned start date cannot be earlier than current date on release record.

I have written the below Onchange client script code, but i am getting the below script error. Please help me on this.

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}

var devRelease = g_form.getDisplayValue('x_thntm_ntrs_devop_devops_release');
//alert('Dev Release :');
if (devRelease) {

var currentDate = new Date();
var startDate = new Date(g_form.getValue('start_date'));

if (startDate.valueOf() < currentDate.valueOf()) {
g_form.addErrorMessage('Error Message Planned start date cannot be earlier than current date');
g_form.setValue('start_date', oldValue);
return false;

}
}

 

Please find the below error message.

Suresh36_0-1682434775954.png

 

 

 

18 REPLIES 18

Sandeep Rajput
Tera Patron
Tera Patron

@Suresh36 here is the corrected version from my side.

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }

    var devRelease = g_form.getDisplayValue('x_thntm_ntrs_devop_devops_release');
    //alert('Dev Release :');
    if (devRelease) {

        var currentDate = new Date();
        var startDate = new Date(g_form.getValue('start_date'));

        if (startDate< currentDate) {
            g_form.addErrorMessage('Error Message Planned start date cannot be earlier than current date');
            g_form.setValue('start_date', oldValue);
            return false;

        }
    }

I have not used valueOf operator in this case as date should be compared in it's absolute form.

 

Tested this script on my PDI and it works fine for me.

Hi,

I have tried this code , same error is getting.

@Suresh36 Can you share the snapshot of your client script here. I would like to verify the configuration done.

var devRelease = g_form.getDisplayValue('x_thntm_ntrs_devop_devops_release');
//alert('Dev Release :');
if (devRelease) {

var currentDate = new Date();
var startDate = new Date(g_form.getValue('start_date'));

if (startDate< currentDate) {
g_form.addErrorMessage('Error Message Planned start date cannot be earlier than current date');
g_form.setValue('start_date', oldValue);
return false;

}
}

@Suresh36 from the following screenshot 

SandeepRajput_0-1682440841885.png

It seems that your onChange method signature is incorrect, it has onChange_rm_release_start_date_3 in it, can you simple remove the signature and simply use 

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