Restrict the end date field not to select more than 365 days from the start date
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-25-2024 01:18 AM
Hi All,
Having a requirement that,
need to restrict the end date field not to select the date range more than 365 days from the start date.
Need assistance on the onchange script to get this sorted.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-25-2024 01:49 AM
Hi @Community Alums Try below onChange Client Script on End Date field
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue === '') {
return;
}
var startDate = g_form.getValue('start_date');
var endDate = g_form.getValue('end_date');
if (startDate && endDate) {
var startObj = new GlideDateTime(startDate);
var endObj = new GlideDateTime(endDate);
var difference = gs.dateDiff(startObj, endObj, true);
if (difference > 365) {
alert('End date cannot be more than 365 days from the start date.');
g_form.setValue('end_date', '');
}
}
}
Regards,
Sid
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-25-2024 01:49 AM
Hi @Community Alums ,
you can achieve it using onchange of client script,
var startDateField = 'start_date'; // Replace with your actual start date field name
var endDateField = 'end_date'; // Replace with your actual end date field name
// Function to calculate maximum allowed end date
function calculateMaxEndDate(startDate) {
var maxEndDate = new GlideDateTime(startDate);
maxEndDate.addDays(365);
return maxEndDate;
}
// Function to validate end date
function validateEndDate() {
var startDate = g_form.getValue(startDateField);
var endDate = g_form.getValue(endDateField);
if (startDate && endDate) {
var maxEndDate = calculateMaxEndDate(startDate);
var endDateTime = new GlideDateTime(endDate);
if (endDateTime.compareTo(maxEndDate) > 0) {
alert('End date must be within 365 days from the start date.');
g_form.setValue(endDateField, ''); // Clear the end date field
}
}
}
})();
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-25-2024 01:49 AM
Hi @Community Alums,
This is very easily achieved with little to no code by leveraging a UI Policy as shown in below screen shots.
To create a UI policy, right click on the top of the form you wish to apply it - eg Incident form, and go to 'Configure' > 'Ui Policies'
To help others (or for me to help you more directly), please mark this response correct by clicking on Accept as Solution and/or Kudos.
Thanks, Robbie
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-25-2024 01:57 AM
Hi @Community Alums
You can refer below post which has similar requirement
Instead of gdt.addMonths(2) you can use gdt.addDays(365)
or you can use No-code UI policy
Mark it helpful if this helps you to understand. Accept solution if this give you the answer you're looking for
Kind Regards,
Rohila V
2022-25 ServiceNow Community MVP