
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-30-2024 06:14 AM
Hi,
I have a scenario where I have to see that start date should not be greater than end date. For this, I have created two client scripts. I am getting error here. Kindly help.
First Client Script
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
if(newValue )
var start = g_form.getValue('delegation_start');
//g_form.addInfoMessage(start);
var end = g_form.getValue('delegation_end');
//g_form.addInfoMessage(end);
if (start > end) {
g_form.addErrorMessage('Delegation Start date cannot be after End date');
g_form.clearValue('delegation_end');
}
}
Second Client Script
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var start = g_form.getValue('delegation_start');
//g_form.addInfoMessage(start);
var end = g_form.getValue('delegation_end');
//g_form.addInfoMessage(end);
if(start > end){
g_form.addErrorMessage('Delegation Start date cannot be after End date');
g_form.clearValue('delegation_end');
}
}
As soon as I select this,
I am getting the error 'Delegation Start date cannot be after End date'.
Regards
Suman P.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-30-2024 06:40 AM - edited 12-30-2024 06:41 AM
@Community Alums
you need to use return false if validation fails
function onSubmit() {
if ((g_form.getValue('delegation_start') != '') && (g_form.getValue('delegation_end') != '')) {
var start = g_form.getValue('delegation_start');
//g_form.addInfoMessage(start);
var end = g_form.getValue('delegation_end');
//g_form.addInfoMessage(end);
if (start > end) {
g_form.addErrorMessage('Delegation Start date cannot be after End date');
g_form.clearValue('delegation_end');
return false;
}
}
}
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-30-2024 06:25 AM - edited 12-30-2024 06:25 AM
@Community Alums
you can use this single onSubmit rather than 2 onChange
function onSubmit() {
//Type appropriate comment here, and begin script below
g_form.hideFieldMsg('delegation_start');
g_form.hideFieldMsg('delegation_end');
if(g_form.getValue('delegation_start') != '' && g_form.getValue('delegation_end')){
var start = new Date(g_form.getValue('delegation_start')).getTime();
var end = new Date(g_form.getValue('delegation_end')).getTime();
if(end < start){
var message = 'Delegation Start date cannot be after End date';
g_form.showFieldMsg('delegation_start', message, 'error');
g_form.showFieldMsg('delegation_end', message, 'error');
return false;
}
}
}
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-30-2024 06:28 AM
@Community Alums
if you still wish to have 2 onChange then use this
onChange of Delegation End
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
g_form.hideFieldMsg('delegation_end');
var start = g_form.getValue('delegation_start');
var end = g_form.getValue('delegation_end');
var startDt = new Date(getDateFromFormat(start, g_user_date_format)).getTime();
var endDt = new Date(getDateFromFormat(end, g_user_date_format)).getTime();
if (endDt < startDt) {
g_form.showFieldMsg('delegation_end', 'Delegation Start date cannot be after End date', 'error');
g_form.clearValue('delegation_end');
}
}
onChange on Delegation Start
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
g_form.hideFieldMsg('delegation_start');
var start = g_form.getValue('delegation_start');
var end = g_form.getValue('delegation_end');
var startDt = new Date(getDateFromFormat(start, g_user_date_format)).getTime();
var endDt = new Date(getDateFromFormat(end, g_user_date_format)).getTime();
if (endDt < startDt) {
g_form.showFieldMsg('delegation_start', 'Delegation Start date cannot be after End date', 'error');
g_form.clearValue('delegation_start');
}
}
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-30-2024 06:39 AM
Hi @Ankur Bawiskar,
I have used this script, it is giving the error as expected but it is still going to REQ. Kindly help.
function onSubmit() {
if((g_form.getValue('delegation_start') != '') && (g_form.getValue('delegation_end') != ''))
{
var start = g_form.getValue('delegation_start');
//g_form.addInfoMessage(start);
var end = g_form.getValue('delegation_end');
//g_form.addInfoMessage(end);
if(start > end){
g_form.addErrorMessage('Delegation Start date cannot be after End date');
g_form.clearValue('delegation_end');
}
}
}
Regards
Suman P.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-30-2024 06:40 AM - edited 12-30-2024 06:41 AM
@Community Alums
you need to use return false if validation fails
function onSubmit() {
if ((g_form.getValue('delegation_start') != '') && (g_form.getValue('delegation_end') != '')) {
var start = g_form.getValue('delegation_start');
//g_form.addInfoMessage(start);
var end = g_form.getValue('delegation_end');
//g_form.addInfoMessage(end);
if (start > end) {
g_form.addErrorMessage('Delegation Start date cannot be after End date');
g_form.clearValue('delegation_end');
return false;
}
}
}
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader