- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-08-2017 02:19 PM
I am building a scoped application to check out devices. I am using a schedule and schedule entries to set the hours of operation for the checkout station. I am trying to create a script that runs onChange for the Return Time field in the form. On the script include side, I want the return time to be "trimmed" if the default checkout time is outside the checkout station's schedule. Checkouts that span a single schedule can be trimmed simply by adding the result of the GlideSchedule duration method to the checkout time, but checkouts that span multiple schedule events cause the duration method to potentially give a return time that is outside the schedule. Unfortunately, the GlideSchedule API methods do not include a way to measure time between two schedule events. Also, using the GlideSchedule isInSchedule and whenNext methods I'd have to guess and check until a valid return time is found. I noticed that, according to this thread, there is a GlideSchedule method called whenLast, which could work. However, I don't know its arguments. Is there another way to go about this, or does anyone know what the arguments are for whenLast?
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-08-2017 08:11 PM
Now there are 2 ways of interpreting this with a schedule.
- Add 1 day (24 hours) to a date factoring the times the schedule is active. Meaning that a start date time of 2017-07-11 08:00:00 using an 8-5 weekday schedule will result in an end date time of 2017-07-13 14:00:00. 9 hours on 2017-07-11, 9 hours on 2017-07-12, 6 hours on 2017-07-13
2. Add 1 (business) day to 2017-07-11 so that the result is 2017-07-12
You can use the Schedule API like this:
Schedule sched = new Schedule(sys_id of schedule);
GlideDateTime endDateTime = sched.add(startDateTime, duration);
Thanks.
PS: Hit like, Helpful, Correct and Endorse, if it answers your question.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-08-2017 08:11 PM
Now there are 2 ways of interpreting this with a schedule.
- Add 1 day (24 hours) to a date factoring the times the schedule is active. Meaning that a start date time of 2017-07-11 08:00:00 using an 8-5 weekday schedule will result in an end date time of 2017-07-13 14:00:00. 9 hours on 2017-07-11, 9 hours on 2017-07-12, 6 hours on 2017-07-13
2. Add 1 (business) day to 2017-07-11 so that the result is 2017-07-12
You can use the Schedule API like this:
Schedule sched = new Schedule(sys_id of schedule);
GlideDateTime endDateTime = sched.add(startDateTime, duration);
Thanks.
PS: Hit like, Helpful, Correct and Endorse, if it answers your question.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-09-2017 11:46 AM
Thank you for the quick reply. I was able to get what I wanted using your suggestions and a slight modification. For those in the future, here is a snippet of what I did:
trimToSchedule: function (startTime, endTime, schedID){
var sched = new Schedule(schedID);
if(sched.isValid()){
if(sched.isInSchedule(endTime)){
return endTime;
}else{
gs.addInfoMessage("Return Time was outside operation hours and was trimmed to the nearest valid time.");
var duration = sched.duration(startTime, endTime); //This duration allows us to keep the portion of the original duration that was already in the schedule
return sched.add(startTime, duration); //Adding the above duration to the schedule simply returns the nearest time before endTime that was in the schedule
}
}
}
As a side note, make sure you convert startTime and endTime to the timezone the schedule uses or set the timezone of the schedule.
The GlideSchedule API is not really clear on what add(GlideDateTime, GlideDuration) does. The reason I did not use this before is because I interpreted the description to mean that it adds a schedule entry into the specified schedule.