onsubmit client script for comparison not working
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-01-2024 12:54 AM
Hello Experts,
We created two custom fields on case table "start time " and "end time" .
Requirement
1.Start time should be after "opened_at" field. - working below script
2.end time should be before"closed_at" field. - not working
Tried with below script our 1st requirement is working fine
but 2nd requirement is not working can anyone help on this or else any other better way to achieve this?
function onSubmit() {
// Check if both fields have values
if (g_form.getValue('start_time') != '' && g_form.getValue('opened_at') != '' && g_form.getValue('closed_at') != '' && g_form.getValue('end_time') != '') {
var end = new Date(g_form.getValue('start_time')).getTime();
var start = new Date(g_form.getValue('opened_at')).getTime();
var close = new Date(g_form.getValue('closed_at')).getTime();
var endtime = new Date(g_form.getValue('end_time')).getTime();
// Compare the dates
if (end < start) {
var message = 'start time must be after opened date.';
alert(message);
return false; // Prevent form submission
}
if ( endtime < close {
var message = ' before case resolved date.';
alert(message);
return false; // Prevent form submission
}
}
return true; // Allow form submission if validation passes
}
pleaase support to achieve this
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-01-2024 02:03 AM
Hi @akin9 ,
I am not sure if this is the script you have written your instance .I see a mistake in closing brackets:
if ( endtime < close )//this bracket was not closed .please close it and check it
{
var message = ' before case resolved date.';
alert(message);
return false; // Prevent form submission
}
}
Please mark my answer as helpful/correct if it resolved your query

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-01-2024 02:24 AM
Hey
Dates and times are always fun to deal with in ServiceNow. ServiceNow provides a function called 'getDateFromFormat' that will allow you to create a new JS date from a ServiceNow field.
function onSubmit() {
// Check if both fields have values
var dateFields = ['start_time' , 'opened_at' , 'closed_at' , 'end_time'];
var emptyDateField = dateFields.some(function(field){
return g_form.getValue(field) != '';
});
//Exit script if we have an empty field.
//If we want to prevent form submission
//change 'return;' to 'return false;'
if(emptyDateField)
return;
var startDateTime = getDateFromFormat(g_form.getValue('start_time'), g_user_date_time_format);
var openedDateTime = getDateFromFormat(g_form.getValue('opened_at') , g_user_date_time_format);
if(openedDateTime < startDateTime){
g_form.addErrorMessage(getMessage('Start time must be after opened date.'))
return false;
}
var endDateTime = getDateFromFormat(g_form.getValue('end_time'), g_user_date_time_format);
var closedDateTime = getDateFromFormat(g_form.getValue('closed_at') , g_user_date_time_format);
if(endDateTime < closedDateTime){
g_form.addErrorMessage(getMessage('End time before closed.'))
return false;
}
return true;
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-01-2024 04:14 AM
Hi @akin9 ,
Please try below script
function onSubmit() {
//Type appropriate comment here, and begin script below
// Check if both fields have values
alert("Compare")
if (g_form.getValue('start_time') != '' && g_form.getValue('opened_at') != '' && g_form.getValue('closed_at') != '' && g_form.getValue('end_time') != '') {
var end = new Date(g_form.getValue('start_time')).getTime();
var start = new Date(g_form.getValue('opened_at')).getTime();
var close = new Date(g_form.getValue('closed_at')).getTime();
var endtime = new Date(g_form.getValue('end_time')).getTime();
alert("Inside if ")
// Compare the dates
if (end < start) {
var message = 'start time must be after opened date.';
alert(message);
return false; // Prevent form submission
}
if ( endtime < close) {
var message = ' before case resolved date.';
alert(message);
return false; // Prevent form submission
}
}
return true; // Allow form submission if validation passes
}
Please mark my answer correct and helpful if this works for you
Thanks and Regards
Sarthak