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

Deepa Srivastav
Kilo Sage

Below is the code to exclude weekends...might help you...



Mark Correct if it solved your issue or hit Like and Helpful if you find my response worthy.



Thanks,
Deepa



function getDateDiffExcWeekends(start , end){


//Make Sure that start and end are GlideDateTime Objects


  var days = 0 ;


  while (start < end) {


  start.addDays(1);


  if (start.getDayOfWeek() != 6 && start.getDayOfWeek() != 7)           //excluding Weekend


  {


  days++ ;


  }


  }


  return days;


}




Mike Allen
Mega Sage

You can call GlideAjax and process on the server using schedule scripting:



Useful Scheduling Scripts - ServiceNow Wiki


Mike Allen, Actually I have a date field. if the selected date is less than 3 business days another field should be shown. so I created an onchange client script as follows:


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


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


          return;


    }


      var needed_by = g_form.getValue('Needed_By');


  var today = new Date();


      var ajax = new GlideAjax('DurCalcBusinessDay');



  ajax.addParam('sysparm_name','durCalc');


  ajax.addParam('sysparm_strt',needed_by);


  ajax.addParam('sysparm_tdy',today);


  ajax.getXMLWait();


  var answer = ajax.getAnswer();


  if (answer<3)


  {


    alert("error");


  }


}




and this is my script include:



var DurCalcBusinessDay = Class.create();


DurCalcBusinessDay.prototype = Object.extendsObject(AbstractAjaxProcessor, {


durCalc: function() {


  return gs.dateDiff(this.getParameter('sysparm_strt'),this.getParameter('sysparm_tdy'), false);


}


});




it doesn't work. any help would be appreciated.


Thanks,


Soni


jbauguess
Tera Expert

We do this on the server side (for SLAs and due dates), but the same logic applies to client side for forms.



Handles logic for business days on the client side · GitHub



You'll need to maintain that object of holidays.   We used a "holiday" table for our server side implementation of this (along with GlideDateTime instead of the native Javascript Date object).   If you wanted to use AJAX to dynamically store tables, that's a possibility, I just didn't think it was necessary here.   Another important note is that Sunday is "0" in Date and "7" in GlideDateTime.



Try adding that ui script (globally) and playing with it in the console to see if it meets your needs.



Considering this is a conversion I'm not actually using, I recommend updating that code to meet your specific needs.   I removed all server side references, but didn't add any thing to return a date string you'd use on the client, since I don't know how you commonly format dates (yyyy-MM-dd or MM-dd-yyyy, etc).



Hope this is helpful.