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

Pradeep Sharma
ServiceNow Employee
ServiceNow Employee

Hi Mussie,



1.Make sure your script include has client callable checked to true.


2. ga.getXMLWait is not supported for scoped applications. As alternative you have to use getXML(callback) or getXMLAnswer(callback)



Please let me know if you have any questions.


Similarly, if you are in a scoped app, then gs.log() isn't going to work either.



Can you include the entire script include? We can see your method, but there's more to it than that, right?


Mussie
ServiceNow Employee
ServiceNow Employee

Thanks Guys, I have updated the complete script include on the original post. As mentioned earlier, this also works using background script but if I select the same date from the service catalog item, it doesn't work:



var scheduled_date = '2017-06-12';


var schedRec = new GlideRecord('cmn_schedule');


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


  var sched = new GlideSchedule(schedRec.sys_id);


gs.log(schedRec.sys_id);


  var gdt = new GlideDateTime(scheduled_date);



  if (sched.isInSchedule(gdt.getLocalDate())) {



  //return true;


gs.log('true');


  }


  else


  {


// return false;


gs.log('false');


  }


Hi Mussie,



Your client script should be calling ga.getXMLAnswer() with a callback function.



I went to developer.servicenow.com and searched for getXMLAnswer and clicked the first link under the API results.



ServiceNow Developers