- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-02-2024 04:45 AM
Hello Everyone,
I have 2 fields Start and End. Whenever i select any furfure date in start date field , my end date should not be more than 21 days from start date. below is the script i tried with but didn't work:
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-02-2024 05:06 AM - edited 05-02-2024 05:24 AM
Hi, @Vijay Baokar
Try using the Date() object;
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var strtdate = g_form.getValue('start');
var endate = g_form.getValue('end');
if(strtdate && endate){
var strdatetime = new Date(strtdate);
var endatetime = new Date(endate);
var date1_ms = strdatetime.getTime();
var date2_ms = endatetime.getTime();
var msdiff = date2_ms - date1_ms;
var daysdiff = Math.floor(msdiff / (1000 * 60 * 60 * 24));
if(daysdiff > 21)
{
alert('End date can not be more than 21 days from start date.');
g_form.clearValue('end');
}
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-02-2024 05:12 AM
When speaking of web applications, there's a client side and a server side.
One set of APIs is available client side and another set of APIs is available server side.
Scripts running client side cannot directly call APIs that exist only server side.
That is what you have here: an onChange Client Script running client side, trying to call an API - GlideDateTime that is only available server side.
So you need to either only use APIs that are available client side - like the community post shared by @Dhananjay Pawar explains, or you need to do a GlideAjax call to "reach" server side APIs - which is a wrapper around the standard XMLHttpRequest.
There are other client side APIs in other UI paradigms, like Portal, or Next Experience - that help communication with server side - just FYI.
When not sure which API is available in one of the two environments (client side vs. server side), you should go to https://developer.servicenow.com/dev.do and activate the Reference -> APIs -> Client (for available client side APIs) or Reference -> APIs -> Server Scoped or Reference -> APIs -> Server Global (for available server side APIs).

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-02-2024 05:00 AM
Hi,
Refer this article it will help you to resolve your issue.
Thanks.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-02-2024 05:06 AM - edited 05-02-2024 05:24 AM
Hi, @Vijay Baokar
Try using the Date() object;
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var strtdate = g_form.getValue('start');
var endate = g_form.getValue('end');
if(strtdate && endate){
var strdatetime = new Date(strtdate);
var endatetime = new Date(endate);
var date1_ms = strdatetime.getTime();
var date2_ms = endatetime.getTime();
var msdiff = date2_ms - date1_ms;
var daysdiff = Math.floor(msdiff / (1000 * 60 * 60 * 24));
if(daysdiff > 21)
{
alert('End date can not be more than 21 days from start date.');
g_form.clearValue('end');
}
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-02-2024 05:13 AM
@kkrushkov it didn't work.
Note: i have both the fields as date time
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-02-2024 05:23 AM
@Vijay Baokar I updated my answer. Check it now