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.

InSchedule() is not working as expected

Mussie
ServiceNow Employee
ServiceNow Employee

Hi Guys,

I am trying to alert users on a Service Catalog item when they select a public holiday or a weekend. The problem is it is very erratic, sometimes it works but sometimes it doesn't.

Here is the code I have on the instance and the I tried with both getXML and getXMLWait to no avail:

Script Include:

var SNCVisitorNetValidation = Class.create();

SNCVisitorNetValidation.prototype = Object.extendsObject(AbstractAjaxProcessor, {

checkInSchedule: function(){

  var scheduled_date = this.getParameter('sched_date');

  var schedRec = new GlideRecord('cmn_schedule');

  schedRec.get('name','8-6 weekdays excluding holidays');

  var sched = new GlideSchedule(schedRec.sys_id);

  var gdt = new GlideDateTime(scheduled_date);

  if (sched.isInSchedule(gdt)) {

  gs.log('true ' + gdt);

  return true;

  }

  else

  {

  gs.log('false ' + gdt);

  return false;

  }

  },

type: 'SNCVisitorNetValidation'

});

Client Script:

  var ga = new GlideAjax('SNCVisitorNetValidation');

  ga.addParam('sysparm_name','checkInSchedule');

  ga.addParam('sched_date', start_date);

  //ga.getXML(checkInSchedule);

  ga.getXMLWait();

  answer = ga.getAnswer();

  if(answer == 'true')

  {

  alert ('working day');

  }

  else if (answer == false')

  {

  alert ('holiday');

  }

Could you please check and let me know if you can spot anything?

Mussie

P.S. BTW, if I test this using the Background script it works

1 ACCEPTED SOLUTION

That brings up a good question... where did start_date come from? Should that be:



ga.addParam('sysparm_sched_date', g_form.getValue('start_date'));



Instead?


View solution in original post

15 REPLIES 15

surya76
Tera Contributor
Hi,
please find me a solution for the below query.

*** Script: userTimezonesun.util.calendar.ZoneInfo[id="Europe/Dublin",offset=0,dstSavings=3600000,useDaylight=true,transitions=227,lastRule=java.util.SimpleTimeZone[id=Europe/Dublin,offset=0,dstSavings=3600000,useDaylight=true,startYear=0,startMode=2,startMonth=2,startDay=-1,startDayOfWeek=1,startTime=3600000,startTimeMode=2,endMode=2,endMonth=9,endDay=-1,endDayOfWeek=1,endTime=3600000,endTimeMode=2]]
 
*** Script: startdateofchgci 2024-06-01 15:00:00
*** Script: enddataeofchgci 2024-06-01 16:00:00
*** Script: FOUND IN SCHEDULES:
 
*** Script: userTimezonesun.util.calendar.ZoneInfo[id="IST",offset=19800000,dstSavings=0,useDaylight=false,transitions=7,lastRule=null]
*** Script: startdateofchgci 2024-06-01 15:00:00
*** Script: enddataeofchgci  2024-06-01 16:00:00
*** Script: NOT FOUND IN SCHEDULES:
 
Schedule dates:
 
start date time - 01/05/2024 14:00:00
 
end date time - 01/05/2024 16:00:00
 
the script is same 
the change request date which i pass  are same  even when changing the user timezone 
iam passing only GMT value of start and end dates so it will be same always
 
but why different outputs?
 
with 1 minute difference it is now working as expected why?
 
*** Script: userTimezonesun.util.calendar.ZoneInfo[id="IST",offset=19800000,dstSavings=0,useDaylight=false,transitions=7,lastRule=null]
*** Script: startdateofchgci2024-06-01 14:59:00
*** Script: enddataeofchgci2024-06-01 16:00:00
*** Script: FOUND IN SCHEDULES:
 

 

var ga = gs.getSession().getTimeZone();
            gs.info('userTimezone'+ga);
           
            var cor = new GlideRecord('x_novrp_it_it_change');
            cor.addQuery('sys_id', '96fa52c98786461049aa0d030cbb359c');
            cor.query();
            if (cor.next()) {
                var startdateofchgci = cor.start_date;
                var enddataeofchgci = cor.end_date;
                gs.info('startdateofchgci'+startdateofchgci); 
                gs.info('enddataeofchgci'+enddataeofchgci);
            }

            var scheduleEntryGRsoft = new GlideRecord('cmn_schedule_span');
            scheduleEntryGRsoft.addQuery('schedule', 'b257e7a11b398e5884e2caa16b4bcb62');
            scheduleEntryGRsoft.query();
            if (scheduleEntryGRsoft.next()) {
                var schedulesysid = scheduleEntryGRsoft.schedule;
                var recurrencerule = scheduleEntryGRsoft.schedule;
                var schedule = new GlideSchedule(recurrencerule);
schedule.setTimeZone('Europe/Dublin');
                var gdt_start = new GlideDateTime(startdateofchgci);
                var gdt_end = new GlideDateTime(enddataeofchgci);
                var dur = schedule.duration(gdt_start, gdt_end);
                var schedule_time = dur.getNumericValue() / 1000;
                if ((schedule.isInSchedule(gdt_start)) || (schedule.isInSchedule(gdt_end)) || (schedule_time > 0)) {
                      gs.info('FOUND IN SCHEDULES:');
                   
                }else{
                  gs.info('NOT FOUND IN SCHEDULES:');
                }
            }