GlideSchedule Question

kemmy1
Tera Guru

I have a simple script I use in a scheduled job for getting the number of days something is in a certain state on our Change Request form:

var ga = new GlideRecord('metric_instance');
ga.addEncodedQuery("definition=601d0029dbdd1340d8ae30ca7c961929^value=Approved for Implementation^startISNOTEMPTY^endISEMPTY");
ga.query();
   while (ga.next()) {
   var begin = ga.start.getDisplayValue();
   var end = gs.now();
   var difference = gs.dateDiff(begin, end, false);
       var ch = new GlideRecord('change_request');
       ch.addQuery('sys_id', ga.id);
       ch.query();
         if(ch.next()){
        ch.u_no_of_days_afi.setValue(difference);
        ch.update();
        }
}

I have to do this because the metric_instance doesn't calculate the number of days unless the state changes to the next state.

Well, my customer is requesting only gathering number of BUSINESS DAYS a state has been in.

I looked into the GlideSchedule option and I'm not getting back what I would expect.  I'm taking it step by step:

I put this into a background script:

var startDate = new GlideDateTime('2018-10-01 08:00:00');
var endDate = new GlideDateTime('2018-10-22 11:00:00');

var schedule = new GlideSchedule();
schedule.load('a09291ecdb394f003a8034cc7c96191d');

var duration = schedule.duration(startDate , endDate );
gs.info(duration.getDurationValue());

My schedule is a simple schedule  (0700 - 1600 (M-F))

gs.info brings back: 

*** Script: 5 15:00:00
There's certainly more than 5 business days between 10/1 and 10/22, so I don't understand why I'm getting that back.

LIsa

 

1 ACCEPTED SOLUTION

If you want to display business days, then you have to add in the missing hours:

var startDate = new GlideDateTime('2018-10-01 08:00:00');
var endDate = new GlideDateTime('2018-10-22 11:00:00');

var schedule = new GlideSchedule();
schedule.load('a09291ecdb394f003a8034cc7c96191d');

var duration = schedule.duration(startDate , endDate );
gs.info(duration.getDurationValue());

//convert to business days
var seconds = (duration.getNumericValue()/(1000));
var businessDays = Math.floor(seconds / (60*60*9));
//add the remaining 15 hours/per day
var newDuration = new GlideDuration(((businessDays * (60*60*15)) + seconds) * 1000);
gs.info(newDuration.getDurationValue());

View solution in original post

8 REPLIES 8

Jim Coyne
Kilo Patron

Try it with a smaller window to be sure, but the "5 15:00:00" is a duration, which works out like this:

(5 x 24 hours) + 15 hours = 135 hours total duration

135 hours / your 9 working hours in a day = 15 working days

Did that make sense?

Yes it does, but how can I (with the original script) get it to business days?

 

 

If you want to display business days, then you have to add in the missing hours:

var startDate = new GlideDateTime('2018-10-01 08:00:00');
var endDate = new GlideDateTime('2018-10-22 11:00:00');

var schedule = new GlideSchedule();
schedule.load('a09291ecdb394f003a8034cc7c96191d');

var duration = schedule.duration(startDate , endDate );
gs.info(duration.getDurationValue());

//convert to business days
var seconds = (duration.getNumericValue()/(1000));
var businessDays = Math.floor(seconds / (60*60*9));
//add the remaining 15 hours/per day
var newDuration = new GlideDuration(((businessDays * (60*60*15)) + seconds) * 1000);
gs.info(newDuration.getDurationValue());