how to get hold of the next occurrence of a schedule
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-24-2019 09:01 PM
Appreciate sone assistance with schedule and schedule span entries
Background
- Maintenance Schedule
- contains one schedule span entry
- schedule span entry is a 4hr block of time that repeats each week on the same day of week
Via javascript (currently background or fix script, but eventually script include), I need to be able to
- find the next occurrence of the schedule n days after the current date
- for that occurrence get the start and end date / times
Example
- schedule with one span entry, 01:00:00 to 04:59:59 repeating on Mondays
- Current date is Sun 21-Jul-2019
- Want to know the next time this schedule will be active in 14 days time and the start and end dates and times for that occurrence
-
- 14 days takes us to Sun 04-Aug-2019
- next occurrence would be on Mon 05-Aug-2019 at 01:00hrs to 05:00hrs
- need to be able to get the start date and time and end date and time
Looking at the GlideSchedule API, looks as if i can solve most of it via
- Create glidedatetime variable (eg futureDate) for now + 14 days
- use whenNext(futureDate,'specified timezone') to return the # milliseconds from that date to the next start
- calculate the start date time (futureDate + milliseconds from whenNext())
- not sure best way to get the end date/time of the occurrence
I believe this is already solved in ServiceNow as when you use the show Schedule function on a schedule record, each occurrence of the schedule is shown in the calendar view as an "Event" and the start and end date/times are listed. I just haven't determined where the code to do this is. Any thoughts?
thanks,
Steve
- Labels:
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-25-2019 09:42 PM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-26-2019 03:01 AM
Not sure if / where these are stored. I thought that they were calculated at run time. In my case I don't need the next occurrence, but rather the next occurrence after a future date
I have just posted what I think is the solution
Thanks for the response,
Steve
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-26-2019 02:57 AM
some more googling and found the solution on this site! Many, many thanks
So my demo code is below. It will need some cleanup and testing (esp timezone and date addition)
var session = gs.getSession();
var timeZone = session.getTimeZoneName();
var scheduleGUID = 'caa944c2db7a3b40746bc4048a9619ff'; //one specific maintenance schedule
var daysAhead = 21;
gs.log('Session timezone is: ' + timeZone);
var schedule = new GlideSchedule(scheduleGUID);
schedule.setTimeZone(timeZone);
gs.log('Schedule is: ' + schedule.name.toString());
// Start from Today
var gd = new GlideDate();
var gdt = new GlideDateTime();
gdt.setNumericValue(gd.getNumericValue());
// GDT (GlideDateTime)
// Start Date
var sd = new GlideDateTime();
sd.setValue(gdt.getDate() + " 00:00:00");
// End Date
var ed = new GlideDateTime();
ed.setValue(gdt.getDate() + " 23:59:59");
ed.addDaysUTC(daysAhead);
// SDT (GlideScheduleDateTime)
// Start Date
var ssd = new GlideScheduleDateTime(sd); // Start Day
ssd.setTimeZone(timeZone);
// End Date
var sed = new GlideScheduleDateTime(ed); // End Day
sed.setTimeZone(timeZone);
// Get Where Schedule Entries overlap with your Start/End Dates
var scheduleMap = schedule.getTimeMap(sd, ed, timeZone);
var span = new GlideScheduleDateTimeSpan(ssd, sed);
var thisMap = new GlideScheduleTimeMap();
thisMap.addInclude(span);
thisMap.buildMap(timeZone);
overlaps = scheduleMap.overlapsWith(thisMap, timeZone);
overlaps.buildMap(timeZone);
gs.log('Getting spans from now and the next ' + daysAhead + ' days.');
while (overlaps.hasNext()) {
var overlapSpan = overlaps.next();
gs.log("Dates: " + overlapSpan.getStart() + " - " + overlapSpan.getEnd());
}
So what are we planning to do with this.....We are considering creating patching windows for our fleet
- 4 to 5 hour maintenance windows. Each window contains the conditions to identify the CIs included
- Once it is identified that patching is required, provide the user with an option to create change requests for each patching window maintenance schedule X days in the future
- the change request's planned start and planed end would be obtained from the overlap span
- the change request's short description would be generated from the Schedule Name and the overlap dates
- affected CI records will be created from the maintenance window's condition
- the rest of the change requests content would be pulled from a template record
If we proceed with this I'll post an update with other parts of the solution
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-19-2020 10:17 AM
Thank you for posting that. I think this is what I need to get me started on figuring out how to tell users when the amount they have chosen for a charitable deduction from payroll will take effect.