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

Bert_c1
Kilo Patron

Hi,

 

Where is "scheduleID" defined in method 1?  And where are your functions defined? Seems more context is needed.

@Bert_c1 the 3rd line has the scheduleID defined. I was running this in background scripts before moving it to scheduled job.

Thanks
Johnny

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

I see that now, I tested method 1 in Scripts - Background:

 

 

 

  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'));

    }

 

 

and got:

 

 

*** Script: 
Start Date: 2024-06-10 21:50:24
*** Script: End Date: 2024-11-06 20:28:25
*** Script: DC Seconds: 3364105
*** Script: DC Seconds(in days): 38.936400462962965
*** Script: Total Seconds: 12868681
*** Script: Total Seconds(in days): 148.94306712962964
*** Script: Glide Subtract Value: 148 Days 22 Hours 38 Minutes
*** Script: INC0000052
*** Script: 
Start Date: 2024-06-17 19:37:49
*** Script: End Date: 2024-11-06 20:28:25
*** Script: DC Seconds: 3207036
*** Script: DC Seconds(in days): 37.11847222222222
*** Script: Total Seconds: 12271836
*** Script: Total Seconds(in days): 142.0351388888889
*** Script: Glide Subtract Value: 142 Days 50 Minutes
*** Script: INC0000018

 

 

So I don't see a problem with the script. I didn't enclose the logic with a "function() {" statement.

 

Your method 2 resulted in:

 

*** Script: INC0000052
*** Script: Start Date: 2024-06-10 21:50:24
*** Script: End Date: 2024-11-07 01:30:57
*** Script: 39 00:00:00
*** Script: 39 Days
*** Script: INC0000018
*** Script: Start Date: 2024-06-17 19:37:49
*** Script: End Date: 2024-11-07 01:30:57
*** Script: 37 04:22:11
*** Script: 37 Days 4 Hours 22 Minutes

Remove the "function () {" lines as they have not names and are not invoked any where.

 

@Bert_c1 if you see the output of method 1 

*** Script: Start Date: 2024-06-10 21:50:24
*** Script: End Date: 2024-11-06 20:28:25
*** Script: Total Seconds(in days): 148.94306712962964

, the total seconds (in days) is coming up as 148 days which is incorrect, it is the calendar days that it is showing and not business days(mon-fri 9x5). Does that make sense? or am I understanding it wrong?

 

Thanks
Johnny

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