ServiceNow

HARSHA GOWDA R
Tera Contributor
function onSubmit() {
    var terminationDate = g_form.getValue('actual_end_date');
    var extensionDate = g_form.getValue('contractor_extension_date');
   
    var currentDate = new Date(); // Getting the current date
 
    // Converting terminationDate to Date object for comparison
    terminationDate = new Date(terminationDate);
    extensionDate = new Date(extensionDate);
 
    // Checking if Termination date is the same as the present date or before the present date
    if (terminationDate <= currentDate) {
        alert('Termination date should be a future date.');
        return false;
    }
 
    // Check if Extension date is before Termination date
    if (extensionDate <= terminationDate) {
        // Prevent the form from being submitted
        alert('Extension date cannot be before or on the Termination date.');
        return false; // This prevents the form from being submitted
    }
 
    // If validation passes, allow the form to be submitted
    return true;
}
What is wrong in this script?
2 REPLIES 2

dhanrajb
Tera Guru

Hi @HARSHA GOWDA R ,

 

'return true' in the last line which will allow the form to submit even it passes the conditions. Try the below script.

 

var submission = true;
var terminationDate = g_form.getValue('actual_end_date');
var extensionDate = g_form.getValue('contractor_extension_date');

var currentDate = new Date(); // Getting the current date

// Converting terminationDate to Date object for comparison
terminationDate = new Date(terminationDate);
extensionDate = new Date(extensionDate);

// Checking if Termination date is the same as the present date or before the present date
if (terminationDate <= currentDate) {
    alert('Termination date should be a future date.');
    submission = false;
}

// Check if Extension date is before Termination date
if (extensionDate <= terminationDate) {
    // Prevent the form from being submitted
    alert('Extension date cannot be before or on the Termination date.');
    submission = false; // This prevents the form from being submitted
}

// If validation passes, allow the form to be submitted
return submission;

 

 

Regards,

Dhanraj

 

Not working