isInSchedule is not working as excepted

AnilREddy1408
Tera Contributor

I added 2017-01-18 in the U.S. Holidays schedule entry list,when i run below code...schedin.isInSchedule(datevalidation)   return false value.

please help me to resolve below issue.

var schedRec = new GlideRecord('cmn_schedule');

        schedRec.addQuery('name', 'U.S. Holidays');

schedRec.query();

if (schedRec.next()) {

      var schedin = new GlideSchedule(schedRec.sys_id);

    var datevalidation = new GlideDateTime();

      datevalidation.setDisplayValue("2017-01-18 06:10:13");

  if (schedin.isInSchedule(datevalidation))

{

          gs.log("Is   in the schedule");

  }

    else

      {

          gs.log("Is NOT in the schedule");

 

      }

}

1 ACCEPTED SOLUTION

Michael Ritchie
ServiceNow Employee
ServiceNow Employee

Date/times can be tricky and the isInSchedule() function can accept a second parameter for a time zone.   If one is not entered then it defaults to the system time zone.   Since you are using setDisplayValue you are likely wanting the user's timezone to be used and then it is comparing it to system, thus not working.   So to solve this you need to get your timezones in sync.



You could set datevalidation to gs.nowDateTime(), UTC time or you could pass in a timezone in the isInSchedule() function like:


if (schedin.isInSchedule(datevalidation, "US/Eastern"))



See this other community post for another example:


isInSchedule() giving unexpected results


View solution in original post

4 REPLIES 4

Michael Ritchie
ServiceNow Employee
ServiceNow Employee

Date/times can be tricky and the isInSchedule() function can accept a second parameter for a time zone.   If one is not entered then it defaults to the system time zone.   Since you are using setDisplayValue you are likely wanting the user's timezone to be used and then it is comparing it to system, thus not working.   So to solve this you need to get your timezones in sync.



You could set datevalidation to gs.nowDateTime(), UTC time or you could pass in a timezone in the isInSchedule() function like:


if (schedin.isInSchedule(datevalidation, "US/Eastern"))



See this other community post for another example:


isInSchedule() giving unexpected results


thanks for reply.



i tried as you suggested,,,with parameter ,



if (schedin.isInSchedule(datevalidation, "US/Eastern")) and i set the my instance   Time zone to US/Eastern.but same result. returning   false.


Hi,


when i set   Holiday schedule entry type as none..then issue is resolved got true value.


Hi AnilKumar,



I am stuck in the same situation wherein the program would always return 'False'. What did you mean when you said - 'when i set   Holiday schedule entry type as none..then issue is resolved got true value.'



This could help me.



Thanks.


Sharad