- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-22-2018 01:35 PM
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
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-30-2018 10:15 PM
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());
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-30-2018 01:52 PM
I still don't understand WHY it does this when I'm trying to calculate the number of business days, so I added a schedule and I'm getting 5 15:00 and not 15 working days.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-30-2018 05:23 PM
The duration field displays a simple length of time in days/hours/minutes/seconds. The length of time for 15 "business days" where there's a 9 hour workday is 135 hours, which breaks down into 5 periods of 24 hours (5 "days") + another 15 hours.
I do not think duration fields can display days/hours/minutes/seconds based on a Schedule unfortunately.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-31-2018 06:18 AM
Thank you so much. GlideDate, GlideSchedule, GlideDuration, ugh. I'm going to really study those things now so I can get them down pat!! I really appreciate your assistance!!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-31-2018 07:05 AM
Good luck with that - I still struggle with them. 🙂