Calculate duration based on schedule

madeves
Kilo Contributor

I need to be able to calculate the difference between two date fields in days based on a schedule (business days).   E.g. If start date is 01/03/2014 and end date is 08/03/2014, I want this answer to be 5 days (as 2 days out of the 7 are weekend days).

 

There is a mention of this on the Wiki but it talks about packages.com and doesn't seem to work.

I have also seen 'GlideSchedule' referenced but not how to use it.

 

Has anyone got any thoughts please? Your help would be much appreciated.

13 REPLIES 13

I have decided to use OOB ServiceNow Schedules , Below is the working code (Business Rule)


function onBefore(current, previous) {


    //This function will be automatically called when this rule is processed.    


  gs.include('DurationCalculator');


  var time_elapsed = executeSample(current.opened_at,current.closed_at);


  var workday = 32400;


  var time_elapsed_1 = time_elapsed/workday;


  time_elapsed = time_elapsed_1.toFixed(1);


  current.turn_around = time_elapsed;


}


function executeSample(sDate,eDate) {


      // First we need a DurationCalculator object.


  var dc = new global.DurationCalculator();


  // The above sample is useful in limited cases. We almost always want to


  // use some schedule in a duration computation, let's load a schedule.


  addSchedule(dc);


  // Compute a duration using the schedule. The schedule


  // specifies a nine hour work day. The output of this is 32400 seconds, or


  // a nine hour span.


  dur = dc.calcScheduleDuration(sDate,eDate);


  gs.info("calcScheduleDuration with schedule with Dynamic Values: " + dur);


  return dur;


}


function addSchedule(durationCalculator) {


  //   Load the "8-5 weekdays excluding holidays" schedule into our duration calculator.


  var scheduleName = "8-5 weekdays";


  var grSched = new GlideRecord('cmn_schedule');


  grSched.addQuery('name', scheduleName);


  grSched.query();


  if (!grSched.next()) {


  gs.log('*** Could not find schedule "' + scheduleName + '"');


  return;


  }


  durationCalculator.setSchedule(grSched.getUniqueValue());


}



check this link for more information


Thank you for replying, but I was looking for a client script. I had to replace the line


var dc = new DurationCalculator(); with var dc = new global.DurationCalculator();


and now it's working.


Hi Christopher ,


I am using your code in calculating dates.


My requirement is, calculate business days between start date and end date in a Record producer. Validate value on submit and populate to another field in RITM. Used script include as provided and made below changes in onSubmit client script


          var stratDate1_0 = g_form.getValue('dependnet_startDate1');


          var endDate1_0 = g_form.getValue('dependnet_endDate1');


          ajaxDateDiff = new GlideAjax('dateTimeUtilAjax');


          ajaxDateDiff.addParam('sysparm_name','getSchedDays');


          ajaxDateDiff.addParam('sysparm_sdt', stratDate1_0);


          ajaxDateDiff.addParam('sysparm_edt', endDate1_0);


          ajaxDateDiff.addParam('sysparm_sid', '86747056370d7500c1f727d2b3990e10');


          ajaxDateDiff.getXMLWait();


          var answers1_0 = ajaxDateDiff.getAnswer();


          alert(answers1_0);



But getting alert as NaN. No sure where the mistake is. Can you please help me in resolving the issue.



Thanks,


Anjali.


madeves
Kilo Contributor

Many thanks for this. I will give it a go and let you know how I get on.