- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-18-2022 01:00 AM
Hi,
i have a requirements as follows:
1: User will be allowed to set the date 1 year from the date of submission. Date cannot less or exceed the 1 year timeline.
2: Users with Knowledge manager roles can modify the "Valid to" variable dates and can select any future date.
for the first one we have a script already in place but its not doing anything:
-------------------------------------------------------------------------------------------------------------
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var cdt = g_form.getValue('valid_to'); //First Date/Time field
var dttype = 'day'; //this can be day, hour, minute, second. By default it will return seconds.
var ajax = new GlideAjax('ClientDateTimeUtils');
ajax.addParam('sysparm_name','getNowDateTimeDiff');
ajax.addParam('sysparm_fdt', cdt);
ajax.addParam('sysparm_difftype', dttype);
ajax.getXML(doSomething);
function doSomething(response){
var answer = response.responseXML.documentElement.getAttribute("answer");
//alert(answer);
if(answer > 365 )
{
g_form.showFieldMsg('valid_to','The date needs to be within the next year.','error');
}
}
}
-------------------------------------------------------------------------------------------------------------------------------
please help
Solved! Go to Solution.
- Labels:
-
Knowledge Management

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-18-2022 01:19 AM
Hi,
To validate that, without client script you can just do it with UI policy as below
Condition as below:
In Script tab :
Mark as correct and helpful if it solved your query.
Regards,
Sumanth

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-18-2022 01:20 AM
You can follow below link to easily make it work, script include will not be required.
How to validate date field for checking 1 year validation in client script
There is also one low code way of achieving this(Preferred approach)
No Code date validations through (Catalog) UI Policies
Feel free to mark correct, if your issue has been resolved, so it ends up in solved queue.
Will be helpful for others looking for the similar query.
Aman Kumar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-18-2022 01:18 AM
Hi,
So date cannot exceed 1 year from now?
If yes then why to use script include and client script; it can be done with UI policy directly
Regards
Ankur
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-18-2022 01:21 AM
ok but how does it know the current date then ?
users shouldnt be able to set a date which exceeds 1 year and it should display a message
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-18-2022 03:09 AM
Hi,
update script as this and no script include is required
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
g_form.hideFieldMsg('valid_to');
var futureDate = new Date(new Date().setFullYear(new Date().getFullYear() + 1)).getTime();
var selectedDate = new Date(newValue).getTime();
if(selectedDate > futureDate){
g_form.showFieldMsg('valid_to','The date needs to be within the next year.','error');
}
}
Regards
Ankur
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-18-2022 01:19 AM