I want to have a maxium cutoff date for a date field.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
4 weeks ago
Hi There,
I want to add a date field to a catalog item and want a cut off date where if i fuill in a date before the first of juli or the first of januari it is not possible to fill that date in.
How can i achieve this? I want to use a catalog client script or maybe UI policy.
Please assist me!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
4 weeks ago
I don't know if I'm following the test/use case criteria exactly, but try this approach first:
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
4 weeks ago
Hello @NabilA581920111,
To achieve this in ServiceNow, you can add a date field to your catalog item and then use a Catalog Client Script to enforce the cutoff date logic. Here's how you can do it step-by-step:
Add a Catalog Client Script
- In the same catalog item, go to the Catalog Client Scripts tab.
- Click New.
- Set:
- Type:
onChange
- Variable Name:
requested_date
- Script: Use the following script:
function onChange(control, oldValue, newValue, isLoading) { if (isLoading || newValue == '') { return; } var selectedDate = new Date(newValue); var year = selectedDate.getFullYear(); var janCutoff = new Date(year, 0, 1); // January 1st var julCutoff = new Date(year, 6, 1); // July 1st if (selectedDate < janCutoff || selectedDate < julCutoff) { g_form.showFieldMsg('requested_date', 'Date must be after January 1st and July 1st.', 'error'); g_form.setValue('requested_date', ''); } }
- Type:
If this helped to answer your query, please mark it helpful & accept the solution.
Thanks
Santosh.p
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Hi @NabilA581920111 , I had acheived something similar to this from my recent project.
Here is the onChange catalog client script for your requirement.
Script:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue === '') {
return;
}
var selectedDate = new Date(newValue);
selectedDate.setHours(0, 0, 0, 0);
var today = new Date();
today.setHours(0, 0, 0, 0);
var year = today.getFullYear();
var jan1 = new Date(year, 0, 1); // Jan = month 0
var july1 = new Date(year, 6, 1); // July = month 6var nextCutoff;
if (today < july1) {
nextCutoff = july1; // If we are before July, next cutoff is July 1 of this year
} else {
nextCutoff = new Date(year + 1, 0, 1); // If we are past July, next cutoff is Jan 1 of next year
}if (selectedDate < nextCutoff) {
g_form.addErrorMessage("Please select a date on or after " + nextCutoff.toDateString() + ".");
g_form.clearValue('your_date_variable'); // Replace with your actual variable name
}
}
If this solution helped you Please Mark this solution as accepted and helpful as it will be helpful for other users as well.
Best Regards.
Saurabh V.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
you can use UI policy with no scripting required.
check link shared by @Brad Bowman
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