Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

GlideDate and GlideDateTime.getLocalDate() are not equivalent, why?

Philipp5
Kilo Contributor

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...

3 REPLIES 3

Sukraj Raikhraj
Kilo Sage

https://docs.servicenow.com/bundle/geneva-servicenow-platform/page/script/glide_server_apis/reference/r_GlideDate_GlideDateTime_examples.html

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

the compareTo() function works on GlideDateTime and not on GlideDate object

So you better use the 1st option

find_real_file.png

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Allen Andreas
Tera Patron

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!