- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-25-2022 11:30 PM
Hi everyone,
I need to create a date/time type variable.
And in the date picker, the date should be only future dates and allow only upto 90 days.
Thanks in advance for your help!
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-27-2022 05:01 AM
Hi @Southsayer ,
Then you can use the 2 onchange client scripts.
1. Create onchange client script on start date and use below script
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
if (g_form.getValue('start_date') != '' && g_form.getValue('end_date')) {
var start = new Date(g_form.getValue('start_date')); //your start date variable name
var end = new Date(g_form.getValue('end_date')); //your end date variable name
if (start > end) {
g_form.addErrorMessage('Verify start date and end date');
return false;
}
}
}
2. Create other onchange client script on end date and use below script
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
if (g_form.getValue('start_date') != '' && g_form.getValue('end_date')) {
var start = new Date(g_form.getValue('start_date')); //your start date variable name
var end = new Date(g_form.getValue('end_date')); //your end date variable name
if (end < start) {
g_form.addErrorMessage('Verify start date and end date');
return false;
}
}
}
ServiceNow Community MVP 2024.
Thanks,
Pavankumar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-25-2022 11:39 PM - edited 10-25-2022 11:40 PM
The only way today is to create a catalog client script onChange that will do a asynchronous call to a script include
In this example the script include simply return gs.nowDateTime();
var ajax = new GlideAjax('your script include');
ajax.addParam('sysparm_name','your function');
ajax.getXML(doSomething);
function doSomething(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
if (answer > newValue) {
g_form.clearValue("your field");
g_form.showFieldMsg("field", "Date provided should not be in the past", "error");
return;
}
// and another function to verify the datediff is lower to 90 days
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-26-2022 12:21 AM
Hi @Southsayer ,
To achieve this requirement you can create UI policy with the conditions as shown below.
1. Select your date and time variable on UI policy and use below conditions.
2. you can select only future dates 90 days from now.
Script:
function onCondition() {
alert("Enter valid date, You can select future date upto 90 days from today");
g_form.clearValue('start_date');
}
3.Below is the alert you can see when the date is after 90 days from now and same for when you select past dates.
ServiceNow Community MVP 2024.
Thanks,
Pavankumar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-26-2022 09:13 AM
Hi Pavan,
I want to not show any kind of alerts, just the calendar should only show the above mention dates, could you please help out?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-26-2022 08:12 PM
Hi @Southsayer ,
There is no method for the date variable to make the past/future dates read-only or greyed out.
Only we can do is the validation.
ServiceNow Community MVP 2024.
Thanks,
Pavankumar