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.

Calculate business days

JohnnySnow
Kilo Sage

Hi Team,

 

We have a requirement to send a notification to the end user if

1. Reminder 1 - No update received for the past 10 business days

2. Reminder 2 - No update received for the past 20 business days

3. Reminder 3 - No update received for the past 30 business days

 

I'm struggling to find the "business" days, can someone help me achieve this?

 

below are the codes which I have tried in my PDI and it yields incorrect results.

 

// METHOD 1***

(function() {

    var encQue = 'active=true^state=2';
    var scheduleID = "090eecae0a0a0b260077e1dfa71da828";


    var gr = new GlideRecord('incident');
    gr.addEncodedQuery(encQue);
 gr.orderBy('sys_updated_on');
 gr.setLimit("2");
    gr.query();

    while (gr.next()) {

        var diff;
        var startGDT = new GlideDateTime(gr.getValue('sys_updated_on'));
        var endGDT = new GlideDateTime(gs.nowDateTime());
  var gDiff = GlideDateTime.subtract(startGDT, endGDT);
  var durationCalculator = new DurationCalculator();
  durationCalculator.setSchedule(scheduleID);
  
  diff = durationCalculator.calcScheduleDuration(startGDT, endGDT);
  var dcSeconds = durationCalculator.getSeconds();
  var dcTotalSeconds = durationCalculator.getTotalSeconds();

  gs.info("\nStart Date: " + startGDT );
  gs.info("End Date: " + endGDT);
  
  gs.info("DC Seconds: " + dcSeconds );
  gs.info("DC Seconds(in days): " + dcSeconds / (60 * 60 * 24));

  gs.info("Total Seconds: " + dcTotalSeconds);
  gs.info("Total Seconds(in days): " + dcTotalSeconds / (60 * 60 * 24));

  gs.info("Glide Subtract Value: " + gDiff.getDisplayValue());
  
  gs.info(gr.getValue('number'));

    }
})();

//***//METHOD2
(function () {
 var encQue = 'active=true^state=2';
 // var scheduleID = "090eecae0a0a0b260077e1dfa71da828";
 var gr = new GlideRecord('incident');
 gr.addEncodedQuery(encQue);
 gr.orderBy('sys_updated_on');
 gr.setLimit("2");
 gr.query();

 while (gr.next()) {
  var schedule = new GlideSchedule();

  schedule.load('090eecae0a0a0b260077e1dfa71da828');  
  var startGDT = new GlideDateTime(gr.getValue('sys_updated_on'));
  var endGDT = new GlideDateTime();
  var duration = schedule.duration(startGDT, endGDT);
  
  gs.info(gr.getValue('number'));
  gs.info("Start Date: " + startGDT);
  gs.info("End Date: " + endGDT);
  gs.info(duration.getDurationValue());
  gs.info(duration.getDisplayValue()); 
 }
})();

 

Thanks
Johnny

Please mark this response as correct or helpful if it assisted you with your question.
5 REPLIES 5

Yes, the values from your code are wrong. I've reviewed:

 

https://developer.servicenow.com/dev.do#!/reference/api/washingtondc/server_legacy/c_DurationCalcula...

 

and updated your method 2 code as follows:

 

 

var encQue = 'active=true^state=2';
 // var scheduleID = "090eecae0a0a0b260077e1dfa71da828";
 var gr = new GlideRecord('incident');
 gr.addEncodedQuery(encQue);
 gr.orderBy('sys_updated_on');
 gr.setLimit("2");
 gr.query();

 while (gr.next()) {
  var startGDT = new GlideDateTime(gr.getValue('sys_updated_on'));
  var endGDT = new GlideDateTime();
  // new code, per API documentation
  var duration = new DurationCalculator();
  duration.setSchedule('090eecae0a0a0b260077e1dfa71da828'); // 8-5 weekdays excluding holidays
  duration.calcScheduleDuration(startGDT, endGDT);
  var secs = duration.getSeconds();
  var totalSecs = duration.getTotalSeconds();
  gs.info("***SCHEDULE DURATION: SECS=" + secs + " TOTALSECS=" + totalSecs + " ENDTIME = " + endGDT);
  var noDays = secs / (60*60*24);
  gs.info("noDays = " + noDays);

  // existing code
  gs.info(gr.getValue('number'));
  gs.info("Start Date: " + startGDT);
  gs.info("End Date: " + endGDT);
  gs.info(duration.getDurationValue());
  gs.info(duration.getDisplayValue()); 
 }

 

 

and I get:

 

 

*** Script: ***SCHEDULE DURATION: SECS=3410490.611 TOTALSECS=13023066 ENDTIME = 2024-11-08 15:21:30
*** Script: noDays = 39.47327096064815
*** Script: INC0000052
*** Script: Start Date: 2024-06-10 21:50:24
*** Script: End Date: 2024-11-08 15:21:30
*** Script: undefined
*** Script: undefined
*** Script: ***SCHEDULE DURATION: SECS=3253421.662 TOTALSECS=12426221 ENDTIME = 2024-11-08 15:21:30
*** Script: noDays = 37.65534331018519
*** Script: INC0000018
*** Script: Start Date: 2024-06-17 19:37:49
*** Script: End Date: 2024-11-08 15:21:30
*** Script: undefined
*** Script: undefined

 

 

As you can see, the un-documented methods used in lines 26 and 27 are undefined.  Good luck some simple examples:

var dc = new DurationCalculator();
dc.setSchedule('090eecae0a0a0b260077e1dfa71da828');		//8-5 weekdays excluding holidays

var sdt = new GlideDateTime('2024-11-01 00:00:00');
var edt = new GlideDateTime('2024-11-06 23:59:59');
gs.info('sdt = ' + sdt + ', edt = ' + edt);
dc.calcScheduleDuration(sdt, edt);
var secs = dc.getSeconds();
var totalSecs = dc.getTotalSeconds();
gs.info("***SCHEDULE DURATION: SECS=" + secs + " TOTALSECS=" + totalSecs);
var noDays = secs / (60*60*24);
var noSchDays = secs / (60*60*9);
var noHours = secs / (60*60);
gs.info("noHours = " + noHours + ", noDays = " + noDays + ". noSchDays = " + noSchDays);

var sdt = new GlideDateTime('2024-11-01 00:00:00');
var edt = new GlideDateTime('2024-11-04 23:59:59');
gs.info('sdt = ' + sdt + ', edt = ' + edt);
dc.calcScheduleDuration(sdt, edt);
var secs = dc.getSeconds();
var totalSecs = dc.getTotalSeconds();
gs.info("***SCHEDULE DURATION: SECS=" + secs + " TOTALSECS=" + totalSecs);
var noDays = secs / (60*60*24);
var noSchDays = secs / (60*60*9);
var noHours = secs / (60*60);
gs.info("noHours = " + noHours + ", noDays = " + noDays + ". noSchDays = " + noSchDays);

 

 

Results in:

*** Script: sdt = 2024-11-01 00:00:00, edt = 2024-11-06 23:59:59
*** Script: ***SCHEDULE DURATION: SECS=129600 TOTALSECS=518399
*** Script: noHours = 36, noDays = 1.5. noSchDays = 4
*** Script: sdt = 2024-11-01 00:00:00, edt = 2024-11-04 23:59:59
*** Script: ***SCHEDULE DURATION: SECS=64800 TOTALSECS=345599
*** Script: noHours = 18, noDays = 0.75. noSchDays = 2