- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-18-2025 02:53 AM
When a month is selected in the Month for Adhoc field, the From Date and To Date pickers should be restricted to that selected month. Users should only be able to select dates within the chosen month, preventing navigation to the previous or next month.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-18-2025 10:38 PM
Hello,
I think this is not possible. But as a workaround you can use solution with Client Scripts.
You need to have two onChange Client Scripts (one for start date and second for end date) and one onSubmit Client Script.
- onChange Client Scripts inform user that he selected date outside of Adhoc month.
- onSubmit Client Script aborts submit if dates are outside of Adhoc month.
Example of one onChange Client Script is:
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var adhocMonth = g_form.getValue('u_month_for_adhoc'); // Expects month values as 01,02,03...
var startDateArr = g_form.getValue('start_date').split('-');
var month = startDateArr[1]; // Expects system date format in yyyy-mm-dd hh:mm:ss
if (adhocMonth !== month) {
g_form.showFieldMsg('start_date','Start date must be within Adhoc month.','error');
} else {
g_form.hideFieldMsg('start_date', true);
}
}
Example of onSubmit Client Script is:
function onSubmit() {
var adhocMonth = g_form.getValue('u_month_for_adhoc'); // Expects month values as 01,02,03...
var startDateArr = g_form.getValue('start_date').split('-');
var endDateArr = g_form.getValue('end_date').split('-');
var startMonth = startDateArr[1]; // Expects system date format in yyyy-mm-dd hh:mm:ss
var endMonth = endDateArr[1]; // Expects system date format in yyyy-mm-dd hh:mm:ss
if (adhocMonth !== startMonth) {
g_form.showErrorBox('Start date must be within Adhoc month.', 'error', true);
return false;
} else if (adhocMonth !== endMonth) {
g_form.showErrorBox('End date must be within Adhoc month.', 'error', true);
return false;
}
}
Modify scripts according to your custom fields and values. I don't know what values you using for month in 'Month for Adhoc' field.
If my answer helped you, please mark it as correct and helpful, thank you 👍
Martin
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-18-2025 10:51 PM
Do this
1) Ensure you give the choice values as this for the months in your choice field
Jan - 0
Feb - 1
March - 2
and so on
Then use 1 onChange on From To and 1 onChange on To date
Something like this will handle all the date formats as per logged in user
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
g_form.hideFieldMsg('start_date');
var monthNumber = g_form.getValue('adhocMonthField');
var endDt = new Date(getDateFromFormat(newValue, g_user_date_time_format));
var month = endDt.getMonth(); // returns 0 for Jan, 1 for Feb and so on
if (month != monthNumber) {
g_form.showFieldMsg('start_date', 'Start date must be within Adhoc month.', 'error');
}
}
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
02-18-2025 10:38 PM
Hello,
I think this is not possible. But as a workaround you can use solution with Client Scripts.
You need to have two onChange Client Scripts (one for start date and second for end date) and one onSubmit Client Script.
- onChange Client Scripts inform user that he selected date outside of Adhoc month.
- onSubmit Client Script aborts submit if dates are outside of Adhoc month.
Example of one onChange Client Script is:
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var adhocMonth = g_form.getValue('u_month_for_adhoc'); // Expects month values as 01,02,03...
var startDateArr = g_form.getValue('start_date').split('-');
var month = startDateArr[1]; // Expects system date format in yyyy-mm-dd hh:mm:ss
if (adhocMonth !== month) {
g_form.showFieldMsg('start_date','Start date must be within Adhoc month.','error');
} else {
g_form.hideFieldMsg('start_date', true);
}
}
Example of onSubmit Client Script is:
function onSubmit() {
var adhocMonth = g_form.getValue('u_month_for_adhoc'); // Expects month values as 01,02,03...
var startDateArr = g_form.getValue('start_date').split('-');
var endDateArr = g_form.getValue('end_date').split('-');
var startMonth = startDateArr[1]; // Expects system date format in yyyy-mm-dd hh:mm:ss
var endMonth = endDateArr[1]; // Expects system date format in yyyy-mm-dd hh:mm:ss
if (adhocMonth !== startMonth) {
g_form.showErrorBox('Start date must be within Adhoc month.', 'error', true);
return false;
} else if (adhocMonth !== endMonth) {
g_form.showErrorBox('End date must be within Adhoc month.', 'error', true);
return false;
}
}
Modify scripts according to your custom fields and values. I don't know what values you using for month in 'Month for Adhoc' field.
If my answer helped you, please mark it as correct and helpful, thank you 👍
Martin
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-18-2025 10:48 PM
you can use 2 onChange client scripts 1 for From and 1 for To
Then use GlideAjax and get the date user selected, pass the month name
Then determine the first day of that month and last day of that month
Then check if the user given date falls between this
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
02-18-2025 10:51 PM
Do this
1) Ensure you give the choice values as this for the months in your choice field
Jan - 0
Feb - 1
March - 2
and so on
Then use 1 onChange on From To and 1 onChange on To date
Something like this will handle all the date formats as per logged in user
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
g_form.hideFieldMsg('start_date');
var monthNumber = g_form.getValue('adhocMonthField');
var endDt = new Date(getDateFromFormat(newValue, g_user_date_time_format));
var month = endDt.getMonth(); // returns 0 for Jan, 1 for Feb and so on
if (month != monthNumber) {
g_form.showFieldMsg('start_date', 'Start date must be within Adhoc month.', 'error');
}
}
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