Validating a Date Field Against a Customizable Future Date Range
Requirement:
In ServiceDesk Plus, there may be scenarios where you want to ensure that a user-selected date (through a request UDF field) falls within a valid range — for example, not in the past and not beyond 1 year, 6 months, or a certain number of days from today.
This can be enforced using a Field and Form Rules script.
Steps to Implement:
-
Create two additional date fields and add them to the request template to capture the start and end dates.
-
Navigate to Request Template > Field and Form Rules > On Form Submit, and click New to create a new rule.
-
Paste the provided script into the rule and update it with your specific additional field details. Refer to the comments within the script for guidance.
Script:
- // ===== Configuration =====
- var startDate = "WorkOrder_Fields_UDF_DATE1"; // Replace with start Date - field name
- var endDate = "WorkOrder_Fields_UDF_DATE2"; // Replace with end Date - field name
- var allowedYears = 1; // Set to 0 if not using
- var allowedMonths = 0; // Set to 0 if not using
- var allowedDays = 0; // Set to 0 if not using
- // ==========================
- var startDate_val = $CS.getValue(startDate);
- var endDate_val = $CS.getValue(endDate);
- // var maxAllowedDate = new Date(); // if its from Current Date.
- var maxAllowedDate = startDate_val; // if its from a Start Date.
- // Add allowed duration
- maxAllowedDate.setFullYear(maxAllowedDate.getFullYear() + allowedYears);
- maxAllowedDate.setMonth(maxAllowedDate.getMonth() + allowedMonths);
- maxAllowedDate.setDate(maxAllowedDate.getDate() + allowedDays);
- // Validation
- if (endDate_val > maxAllowedDate) {
- $CS.showInfo("Please select a date within the allowed limit!", { "type": "failure" });
- $CS.setValue(endDate, new Date()); // Reset to today
- }
New to ADSelfService Plus?