- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-11-2023 02:23 AM - edited 04-11-2023 02:24 AM
Hi,
On the change form, I'd like an alert for when a date is selected for the planned start/end date that is before the created date of the change. I have an onChange client script but it's not giving me an alert.
This is my current onChange client script for planned start date (same one for planned end date):
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
//Type appropriate comment here, and begin script below
var type = g_form.getValue('change_type');
var plannedStart = g_form.getValue('start_date');
var opened = g_form.getValue('sys_created_on');
if (type == 'normal' || type == 'standard' || type == 'emergency') {
if (plannedStart < opened) {
alert('Planned start date cannot be before opened date');
return false;
}
}
}
What's wrong with it? Thanks in advance!
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-11-2023 03:22 AM
Hello,
Make sure that created field is displayed in the form and try this script
Nota: in my instance change type field is "type" and not "change_type"
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
//Type appropriate comment here, and begin script below
var type = g_form.getValue('type');
var plannedStart = new Date(getDateFromFormat(newValue, g_user_date_time_format));
var opened = new Date(getDateFromFormat(g_form.getValue('sys_created_on'), g_user_date_time_format));
if (type == 'normal' || type == 'standard' || type == 'emergency') {
if (plannedStart < opened) {
alert('Planned start date cannot be before opened date');
return false;
}
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-11-2023 07:55 AM
Thanks @Upsilon , I think getDatefromFormat did the trick here!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-11-2023 04:47 AM
Have you checked if type values are satisfying the if condition ?

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-11-2023 05:15 AM
Use the following script and let me know if it works for you.
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
//Type appropriate comment here, and begin script below
var type = g_form.getValue('type');
var plannedStart = g_form.getValue('start_date');
var opened = g_form.getValue('sys_created_on');
if (type == 'normal' || type == 'standard' || type == 'emergency') {
if (plannedStart < opened) {
alert('Planned start date cannot be before opened date');
return false;
}
}
}
Also make sure that you have the highlighted fields available on the form in order to make this script work.
In your script, you are fetching the value of change type from
var type = g_form.getValue('change_type');
Where as it should be
var type = g_form.getValue('type');
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-11-2023 07:51 AM
Is sys_created_on field on form? if not then your script won't work
If yes this will work
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
//Type appropriate comment here, and begin script below
var type = g_form.getValue('type');
var plannedStart = new Date(g_form.getValue('start_date')).getTime();
var opened = new Date(g_form.getValue('end_date')).getTime();
if (type == 'normal' || type == 'standard' || type == 'emergency') {
if (plannedStart < opened) {
alert('Planned start date cannot be before opened date');
return false;
}
}
}
If it's not on form then use display BR on change_request table and pass the sys_created_on via g_scratchpad variable
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
//Type appropriate comment here, and begin script below
var type = g_form.getValue('type');
var plannedStart = new Date(g_form.getValue('start_date')).getTime();
var opened = new Date(g_scratchpad.createdTime).getTime(); // pass the g_scratchpad from display BR
if (type == 'normal' || type == 'standard' || type == 'emergency') {
if (plannedStart < opened) {
alert('Planned start date cannot be before opened date');
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
04-11-2023 08:01 AM
Thank you for marking my response as helpful.
As per new community feature you can mark multiple responses as correct.
If my response helped please mark it correct so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader