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.

isInSchedule and GlideDateTime not working as intended

nate_engstrom
Tera Contributor

Hello SN Community,

I am currently trying to resolve an issue regarding sending a notification only within a scheduled window. The client only wants to receive emails between Mon-Fri 8am-6pm. In the code I subtract 5 hours to compensate for the UTC time. However, the isInSchedule resolves to false.

Here is the code I am working with along with the results.

var schedule = new GlideSchedule('cbef152d2bd8660049fe199667da1578', 'US/Eastern');

var date = gs.nowDateTime();

gs.log("NOW DATE FIELD : " + date);

var gdt = new GlideDateTime();

gdt.setDisplayValue(date);

var sub = new GlideTime();

sub.setValue("05:00:00");

gdt.subtract(sub);

gs.log("GLIDE DATE TIME: " + gdt);

if (schedule.isInSchedule(gdt)){

gs.log("RESOLVED TRUE DATE : " + gdt);

answer = true;

}

else{

gs.log("RESOLVED FALSE DATE : " + gdt);

answer = false;

}

Output:

*** Script: NOW DATE FIELD : 2017-12-12 10:27:02 AM

*** Script: GLIDE DATE TIME: 2017-12-12 10:27:02

*** Script: RESOLVED FALSE DATE : 2017-12-12 10:27:02

As you can see, the script is resolving to false even though this date and time are within the desired schedule. I have checked many other community posts regarding this problem before but they did not work for me. Does anyone have a solution to this? Does this have something to do with the way I set up my GlideDateTime object?

I am working with a Helsinki instance as well if that helps at all.

Thank you SN Community!

4 REPLIES 4

dvp
Mega Sage

Can you Try this



var schedule = new GlideSchedule('cbef152d2bd8660049fe199667da1578', 'US/Eastern');




var date = gs.nowDateTime();


gs.log("NOW DATE FIELD : " + date);




if (schedule.isInSchedule(date)){


gs.log("RESOLVED TRUE DATE : " + gdt);


answer = true;


}


else{


gs.log("RESOLVED FALSE DATE : " + gdt);


answer = false;


}


Unfortunately, isInSchedule() does not accept strings which is what gs.nowDateTime() returns.


replace var date = gs.nowDateTime();  


with



var date = new GlideDateTime(gs.nowDateTime())


DhanalakshmM
Tera Contributor