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

Mussie
ServiceNow Employee
ServiceNow Employee

Thanks Chuck for your help, that was the problem.


Another thing which i haven't mentioned, when I pushed the date to the script include, it was being passed as 'date/month/year' even after   instantiating the GlideDateTime. So I had to split it and rearrange it as follows which worked for me but surely there has to be an easier way of doing this??? I didn't want to use UTC related methods because of the day light saving we have in Australia:


var SNCVisitorNetValidation = Class.create();  


SNCVisitorNetValidation.prototype = Object.extendsObject(AbstractAjaxProcessor, {  


checkInSchedule: function(){


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


  var date_parts = scheduled_date.split('/');


  scheduled_date = date_parts[2] + '-' + date_parts[1] + '-' + date_parts[0]


  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);




Any ideas?


Hi Mussie,



I'd do some extra testing on that as it may depend on how the user displays their date field. There are several formats you may encounter like yyyy-mm-dd, So this could be worse than you thought. Welcome to the wonderful world of date and time manipulations.



When I become emperor of the world, there will one universal time, no daylight saving, and everyone will be in sync!


Mussie
ServiceNow Employee
ServiceNow Employee

Thanks Chuck :-), I will make a note of that.


Wallace Roets
ServiceNow Employee
ServiceNow Employee

Hi Mussie,



I had a similar issues a few minutes back and saw this post during my research. Mine seems to be resolved so let's see if this will help you:


Have a look at GlideDateTime - ServiceNow Wiki


This states that: gdt.setDisplayValue(asDisplayed) Sets a date and time value using the current user's display format and time zone.


So have you tried the following in your script include to see if that works:



var SNCVisitorNetValidation = Class.create();    


SNCVisitorNetValidation.prototype = Object.extendsObject(AbstractAjaxProcessor, {    


checkInSchedule: function(){


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


  var gd = new GlideDate();   //or var gdt = newGlideDateTime if the time is also important


  gd.setDisplayValue(scheduled_date); //oppose to GlideDateTime(scheduled_date);


  //then use gd as your new date object


  //... rest of your code


Mussie
ServiceNow Employee
ServiceNow Employee

Hi Wallace,


By using setDisplayValue, I didn't need to manipulate the date to convert it to a different format, this takes care of it. Thanks for your help.


Cheers,


Mussie