- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-05-2025 12:29 AM
Date validation should ensure the value is within 12 months from the current date.
Users have different date format preferences (e.g., YYYY-MM-DD, MM/DD/YYYY), leading to script failures.
Script works for some users and fails for others due to format mismatch.
Solved! Go to Solution.
- Labels:
-
Change Management
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-05-2025 06:50 PM
Always convert the date to a standard format (e.g., yyyy-MM-dd) in your script, regardless of the user preference.
// Example: Convert input date to standard format (in GlideScript)
var inputDate = new GlideDateTime(current.your_date_field);
var formattedDate = inputDate.getDisplayValue(); // or use inputDate.getValue() for system format (yyyy-MM-dd)
Then perform your validation logic using GlideDateTime
Example:
// Assume 'your_date_field' is the date field you want to validate
(function executeRule(current, gS, gRequest, gResponse, gProcessor, gs) {
// Get the date from the record (ALWAYS use getValue() to avoid user format issues)
var inputDate = new GlideDateTime(current.your_date_field.getValue());
// Get today's date
var today = new GlideDateTime();
// Calculate date 12 months ago
var pastDate = new GlideDateTime();
pastDate.addMonthsUTC(-12); // subtract 12 months from today
// Validation logic: check if inputDate is between pastDate and today
if (inputDate.before(pastDate) || inputDate.after(today)) {
gs.addErrorMessage("Date must be within the last 12 months.");
current.setAbortAction(true); // Stops the insert/update
}
})(current, g_scratchpad, g_request, g_response, g_processor, gs);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-06-2025 03:45 AM
I believe I also shared a correct and good answer with no-code approach.
As per new community feature you can mark multiple responses as correct.
If my response helped please mark it correct as well 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
08-05-2025 06:14 AM
Hi @spandanaait
So you want to test if the date falls within 12 months? You can use a UI Policy to handle that."
If my response proves useful, please indicate its helpfulness by selecting " Accept as Solution" and " Helpful." This action benefits both the community and me.
Regards
Dr. Atul G. - Learn N Grow Together
ServiceNow Techno - Functional Trainer
LinkedIn: https://www.linkedin.com/in/dratulgrover
YouTube: https://www.youtube.com/@LearnNGrowTogetherwithAtulG
Topmate: https://topmate.io/atul_grover_lng [ Connect for 1-1 Session]
****************************************************************************************************************
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-05-2025 06:15 AM
you should use UI policy for this and it will take care of the date format and you need not worry on it.
No Code date validations through (Catalog) UI Policies
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
08-06-2025 03:45 AM
I believe I also shared a correct and good answer with no-code approach.
As per new community feature you can mark multiple responses as correct.
If my response helped please mark it correct as well 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
08-05-2025 06:50 PM
Always convert the date to a standard format (e.g., yyyy-MM-dd) in your script, regardless of the user preference.
// Example: Convert input date to standard format (in GlideScript)
var inputDate = new GlideDateTime(current.your_date_field);
var formattedDate = inputDate.getDisplayValue(); // or use inputDate.getValue() for system format (yyyy-MM-dd)
Then perform your validation logic using GlideDateTime
Example:
// Assume 'your_date_field' is the date field you want to validate
(function executeRule(current, gS, gRequest, gResponse, gProcessor, gs) {
// Get the date from the record (ALWAYS use getValue() to avoid user format issues)
var inputDate = new GlideDateTime(current.your_date_field.getValue());
// Get today's date
var today = new GlideDateTime();
// Calculate date 12 months ago
var pastDate = new GlideDateTime();
pastDate.addMonthsUTC(-12); // subtract 12 months from today
// Validation logic: check if inputDate is between pastDate and today
if (inputDate.before(pastDate) || inputDate.after(today)) {
gs.addErrorMessage("Date must be within the last 12 months.");
current.setAbortAction(true); // Stops the insert/update
}
})(current, g_scratchpad, g_request, g_response, g_processor, gs);