
- 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:42 AM
Hi @Community Alums ,
Use below onchange client script for both.
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var start = g_form.getValue('delegation_start');
var end = g_form.getValue('delegation_end');
if (start && end) { // Ensure both dates are present
if (new Date(start) > new Date(end)) { // Compare Date objects
g_form.addErrorMessage('Delegation Start date cannot be after End date');
g_form.clearValue('delegation_end'); // Clear the invalid End Date
}
}
}
tested it, working as expected.
-------------------------------------------------------------------------
If you found my response helpful, please consider selecting "Accept as Solution" and marking it as "Helpful." This not only supports me but also benefits the community.
Regards
Runjay Patel - ServiceNow Solution Architect
YouTube: https://www.youtube.com/@RunjayP
LinkedIn: https://www.linkedin.com/in/runjay
-------------------------------------------------------------------------
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-30-2024 06:44 AM
Hi @Community Alums
Thank you for marking my solution as helpful! The community now supports multi-solution acceptance, allowing you to accept multiple answers.