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.

calculating business days

sonita
Giga Guru

How to calculate business days on a catalog form?

1 ACCEPTED SOLUTION

Soni,



  Add alert in your call back function as shown below and let me know what you see.


    Here is your onChange client script on Needed By   variable:


function onChange(control, oldValue, newValue, isLoading) {


  if (isLoading || newValue == '') {


  g_form.setDisplay('title', false); // put in the variable name you want to show or hide according needed by date


  return;


  }


  var ga = new GlideAjax('ValidateNeededBy');


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


  ga.addParam('sysparm_needed_by',newValue);


  ga.getXML(callBack);



  function callBack(response) {


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


alert(answer);


  if(parseInt(answer)>=27){


  g_form.setDisplay('title', false); // put in the variable name you want to show or hide according needed by date


  }


  else{


  g_form.setDisplay('title', true); // put in the variable name you want to show or hide according needed by date



  }


  }


}


View solution in original post

23 REPLIES 23

Sure. I have a field called "Needed by" on a catalog item form as follows:


find_real_file.png


if the selected date is less than 3 business days , a field for justification is going to be shown, saying : needed by date is earlier than the required lead time, please supply justification:



find_real_file.png


for example if I select today , it is less than 3 business day , so that field should be shown


Soni,



    You will have to use GlideAjax for this. Give me the variable name of "Needed By", I will   get back to you with the scripts. Your "Needed By" variable type is Date/Time right?



Thanks,


Abhinay



PS: Hit like, Helpful or Correct depending on the impact of the response


Thanks a lot. the name is Needed_By and the type is date.


Soni,



    Here is your onChange client script on Needed By   variable:


function onChange(control, oldValue, newValue, isLoading) {


  if (isLoading || newValue == '') {


  g_form.setDisplay('title', false); // put in the variable name you want to show or hide according needed by date


  return;


  }


  var ga = new GlideAjax('ValidateNeededBy');


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


  ga.addParam('sysparm_needed_by',newValue);


  ga.getXML(callBack);



  function callBack(response) {


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


  if(parseInt(answer)>=27){


  g_form.setDisplay('title', false); // put in the variable name you want to show or hide according needed by date


  }


  else{


  g_form.setDisplay('title', true); // put in the variable name you want to show or hide according needed by date



  }


  }


}



Create a script include and name it as "ValidateNeededBy" only. Check the client callable checkbox and copy the following script as is.


script:


var ValidateNeededBy = Class.create();


ValidateNeededBy.prototype = Object.extendsObject(AbstractAjaxProcessor, {


  validateDate: function(){


  var dc = new DurationCalculator();


  dc.setSchedule('08fcd0830a0a0b2600079f56b1adb9ae'); // this is the sys_id of 8-5 weekday schedule in your instance. You can put in the sys_id of the schedule you want to use


  dc.setTimeZone('GMT');


  var dur = dc.calcScheduleDuration(gs.now(),this.getParameter('sysparm_needed_by'))/3600;


  return dur.toString();


  },


  type: 'ValidateNeededBy'


});




Thanks,


Abhinay



PS: Hit like, Helpful or Correct depending on the impact of the response


Thank you so much Abhinay E , but unfortunately doesn't work!