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

Mussie
ServiceNow Employee
ServiceNow Employee

Hi Chuck,


Thanks for the reply. Yes, I tried that already and it doesn't work.


var ga = new GlideAjax('SNCVisitorNetValidation');


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


  ga.addParam('sched_date', start_date);


  ga.getXMLAnswer(checkInSchedule);


  function checkInSchedule (response)



  {


  var answer = response.responseXML.documentElement.getAttribute("answer");




  if(answer == 'true')


  {


        alert('working day');


}


else if (answer == 'false');


{


alert ('holiday');


}


}





aler


Mussie


If you are using getXMLAnswer(), then the xpath walking is already done for you. You don't need the var answer= line.



Something like this (untested, but fixed a syntax error.)



var ga = new GlideAjax('SNCVisitorNetValidation');  


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


  ga.addParam('sched_date', start_date);  


  ga.getXMLAnswer(checkInSchedule);


     


  function checkInSchedule (answer) {  


  if(answer == 'true') {  


        alert('working day');


  } else {  


  alert ('holiday');


  }  


}  


Hi Mussie,



There is error in your client script. Line 3 should be ga.addParam('sysparm_sched_date', start_date); instead of ga.addParam('sched_date', start_date);


Script :


var start_date = g_form.getValue('start_date');


var ga = new GlideAjax('SNCVisitorNetValidation');


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


  ga.addParam('sysparm_sched_date', start_date);


  ga.getXMLAnswer(checkInSchedule);


   


  function checkInSchedule (answer) {


  if(answer == 'true') {


        alert('working day');


  } else {


  alert ('holiday');


  }


}



Please test and let me know.


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?


Assuming start_date value is passed as whole CS is not pasted above