GlideDate and GlideDateTime.getLocalDate() are not equivalent, why?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-07-2021 06:31 AM
Hi everyone, I am new to ServiceNow and currently doing the developer fundamantels.
https://developer.servicenow.com/dev.do#!/learn/learning-plans/quebec/new_to_servicenow/app_store_le...
there is a challenge to prevent users from submitting same date requests.
This works:
(function executeRule(current, previous /*null when async*/ ) {
var rightnow = new GlideDateTime();
var today = rightnow.getLocalDate();
var whenNeeded = new GlideDateTime(current.u_when_needed);
var isToday = whenNeeded.getLocalDate();
if (isToday.compareTo(today) == 0) {
gs.addErrorMessage("You cannot submit NeedIt requests for today.");
current.setAbortAction(true);
}
})(current, previous);
whereas this does not:
(function executeRule(current, previous /*null when async*/ ) {
var today = new GlideDate();
var whenNeeded = new GlideDateTime(current.u_when_needed);
var isToday = whenNeeded.getLocalDate();
if (isToday.compareTo(today) == 0) {
gs.addErrorMessage("You cannot submit NeedIt requests for today.");
current.setAbortAction(true);
}
})(current, previous);
I expected both of these to work, since getLocalDate() should return a GlideDate Object. as in the API: https://developer.servicenow.com/dev.do#!/reference/api/quebec/server/no-namespace/c_APIRef#r_Scoped...
- Labels:
-
Studio

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-07-2021 07:00 AM
https://docs.servicenow.com/bundle/geneva-servicenow-platform/page/script/glide_server_apis/reference/r_GlideDate_GlideDateTime_examples.html
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-07-2021 07:18 AM

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-07-2021 07:28 AM
Hi,
The compareTo function works for either GlideDate or GlideDateTime, but they must be of the same type.
var date1 = new GlideDate();
var date2 = new GlideDate();
gs.info(date1.compareTo(date2));
//works
var date11 = new GlideDateTime();
var date22 = new GlideDateTime();
gs.info(date11.compareTo(date22));
//works
var date111 = new GlideDateTime();
date111 = date111.getLocalDate();
var date222 = new GlideDate();
gs.info(date111.compareTo(date222));
//doesn't work
Please mark reply as Helpful/Correct, if applicable. Thanks!
Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!