- 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:13 AM - edited 05-02-2024 05:55 AM
Chilled. 🙂
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-02-2024 05:17 AM
I wanted to say Date() chill a bit 🙂
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-02-2024 05:54 AM
OK, that is different, but still not correct. 😁
The correct answers is in the post linked by @Dhananjay Pawar: using function getDateFromFormat and "constant" g_user_date_time_format.
You see a user can select date/time formats for themselves that cannot be interpret by Date, and thus it will lead to intermittent functioning.
E.g.
var d = new Date('10.04.2024 13:30:33')
console.log(d);
prints
Fri Oct 04 2024 13:30:33 GMT+0300
but if the current user has selected for himself this as the German date/time format then it should be interpreted as
Wed Apr 10 2024 13:30:33 GMT+0300
- 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).