Validating a Date Field Against a Customizable Future Date Range

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:
  1. Create two additional date fields and add them to the request template to capture the start and end dates.

  2. Navigate to Request Template > Field and Form Rules > On Form Submit, and click New to create a new rule.

  3. 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:
  1. // ===== Configuration =====
  2. var startDate = "WorkOrder_Fields_UDF_DATE1";  // Replace with start Date - field name
  3. var endDate = "WorkOrder_Fields_UDF_DATE2";  // Replace with end Date - field name

  4. var allowedYears = 1;      // Set to 0 if not using
  5. var allowedMonths = 0;     // Set to 0 if not using
  6. var allowedDays = 0;       // Set to 0 if not using
  7. // ==========================

  8. var startDate_val = $CS.getValue(startDate);
  9. var endDate_val = $CS.getValue(endDate);

  10. // var maxAllowedDate = new Date(); // if its from Current Date.
  11. var maxAllowedDate = startDate_val;  // if its from a Start Date.

  12. // Add allowed duration
  13. maxAllowedDate.setFullYear(maxAllowedDate.getFullYear() + allowedYears);
  14. maxAllowedDate.setMonth(maxAllowedDate.getMonth() + allowedMonths);
  15. maxAllowedDate.setDate(maxAllowedDate.getDate() + allowedDays);

  16. // Validation
  17. if (endDate_val > maxAllowedDate) {
  18.     $CS.showInfo("Please select a date within the allowed limit!", { "type": "failure" });
  19.     $CS.setValue(endDate, new Date());  // Reset to today
  20. }

                  New to ADSelfService Plus?