Validate date and time on a due date field that the entered value is not in the Past
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-22-2023 01:21 AM
Hi Team,
I have a requirement to validate the entered date and Time is not in the past on the incident due date field.
I've tried to achieve this via Client Script and a Script Include.
The onChange Client script I have written on change of due date:
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-22-2023 06:26 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-22-2023 06:35 AM
I shared solution below 4 hours ago.
Did you check that?
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-22-2023 02:06 AM
try this client script and also make that field as mandatory.
if you don't make it mandatory the user can submit form even with wrong date
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
if(g_form.getValue('due_date') != ''){
var nowTime = new Date().getTime();
var dueDate = new Date(g_form.getValue('due_date')).getTime();
if(dueDate < nowTime){
g_form.addErrorMessage('Please ensure that the Due date is in the future');
g_form.clearValue('due_date');
g_form.setMandatory('due_date');
}
}
}
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
08-22-2023 06:51 AM
Hi @Ankur Bawiskar ,
The script you have provided is giving the below result as attached when printed.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-22-2023 06:52 AM