Catalog UI Policy to Restrict Date Selection- Script available as an alternate?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Tuesday
We have a catalog item for requesting access to VPN. For offsite contractors we grant temporary access for a time period of no longer than 365 days (1 year) from selected start date.
I have been using the catalog item UI policy with the condition of:
access_end_date is more than 1 year after access_start_date
Then if they try to input an end date more than 1 year the error message pops up stopping them and it is a required field, so they have to use a date earlier.
For example, if they put in a start date of 10/1/2026 they have to input an end date of 09/30/2026 or earlier. If they try to input 10/2/2027, they get the error message and the field does not populate.
The related UI Action for that field is that it goes to Mandatory and the Value Action is clear field and the field message warning is displayed.
This UI Policy / UI Action has started to fail every once in a while, more since we upgraded to Yokohama. We tried changing it to 12 months, but then it doesn't let you select any date in the same month but the next year of the start date. For example, if they input 10/15/2025 as the start date, they cannot input any date in October of 2026 that is earlier than 10/15.
So asking if anyone has a script we can use or an alternate methodology to ensure this conditions is met without failure on a continuous basis.
Thanks!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Wednesday
Greetings,
If you are looking for a Script approach, I'd recommend looking into the GlideDateTime API. You will need two new records. You will need a Client Script that does a GlideAjax Call and a Script Include that will perform the GlideDateTime logic. I'll include the script below and some additional links with helpful information. I hope this information helps you.
GlideDateTime API: GlideDateTime - Global
GlideAjax Cheat Sheet: GlideAjax Example Cheat Sheet (UPDATED) - ServiceNow Community
Client Script:
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
// Exit if the form is just loading or if the new value is empty
if (isLoading || newValue === '') {
return;
}
var startDate = g_form.getValue('field_a');
var endDate = g_form.getValue('field_b');
// Only run if both fields have a value
if (startDate && endDate) {
var ga = new GlideAjax('DateValidator');
ga.addParam('sysparm_name', 'isWithin12Months');
ga.addParam('sysparm_start_date', startDate);
ga.addParam('sysparm_end_date', endDate);
ga.getXML(function(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
if (answer == 'false') {
g_form.showFieldMsg(
'field_b',
'Field B cannot be more than 12 months after Field A.',
'error'
);
// Optionally, clear the invalid value
// g_form.setValue('field_b', '');
} else {
g_form.hideFieldMsg('field_b');
}
});
}
}
Script Include
var DateValidator = Class.create();
DateValidator.prototype = Object.extendsObject(AbstractAjaxProcessor, {
// Check if fieldB is within 12 months of fieldA
isWithin12Months: function() {
// Get the parameters from the GlideAjax call
var startDateStr = this.getParameter('sysparm_start_date');
var endDateStr = this.getParameter('sysparm_end_date');
// Return true if either date is empty (validation not possible)
if (!startDateStr || !endDateStr) {
return true;
}
// Create GlideDateTime objects for the dates
var startDate = new GlideDateTime(startDateStr);
var endDate = new GlideDateTime(endDateStr);
// Add 12 months to the start date
startDate.addMonthsUTC(12);
// Compare the two dates
if (endDate.after(startDate)) {
// End date is more than 12 months after start date
return 'false';
}
return 'true';
},
type: 'DateValidator'
});
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Wednesday
I will try these two options in our Dev environment and see if it works.
If it does, I will go back and mark this as "Accept as Solution" and "Helpful"
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Wednesday
strange that the same script is not working after upgrade?
any patterns you found?
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
