Challenge: Adjusting cmn_schedule_span dates from on-call schedule to a request date

qwertyflop
Giga Guru

When a Schedule is created on an On-Call Schedule, there is one cmn_schedule_span created for it that contains the datetimes that were provided as the start and end time when defining the schedule. I am working on a request that takes a time window in the form of two date fields. When submitted, it runs a lot of code to find ANY member of the on-call group that would be working, not just the on-call people. Then it does more checks on other things related to the user to see if they can take the request.

 

The issue that I am having is with this specific piece of the logic. Because there is only one cmn_schedule_span record created, I am trying to take those dates and adjust them so that they are relevant to the request start time. I have left in a commented-out block of code that I tried to add to fix the issue, and an explanation of why it isn't enough. Has anyone tried to do something like this before? Is there an OOB method of doing what I am trying here?

 

// These variables represent the start and end datetimes entered as the request window.
var _reqStart = new GlideDateTime();
var _reqEnd = new GlideDateTime();
_reqStart.setDisplayValue('03/03/2023 11:00:00 PM');
_reqEnd.setDisplayValue('03/03/2023 11:30:00 PM');
// _reqStart.setDisplayValue('03/03/2023 04:00:00 AM');
// _reqEnd.setDisplayValue('03/03/2023 04:30:00 AM');

// These variables represent the display value of the start_date_time and end_date_time on the cmn_schedule_span record that is created for each schedule defined on an On-Call Schedule.
var _schedStartDisplayValue = "02/02/2023 10:00:00 PM";
var _schedEndDisplayValue = "02/03/2023 07:00:00 AM";

var res = _getScheduleStartEndForDay(_reqStart, _schedStartDisplayValue, _schedEndDisplayValue);

gs.info(res.start.getDisplayValue() + " <= " + _reqStart.getDisplayValue() + " <= " + res.end.getDisplayValue());
gs.info(res.start <= _reqStart);
gs.info(_reqStart <= res.end);

function _getScheduleStartEndForDay(reqStart, schedStartDisplayValue, schedEndDisplayValue) {
    // Create GDT from schStartDisplayValue
    var schedStart = new GlideDateTime();
    schedStart.setDisplayValue(schedStartDisplayValue);

    // Create GDT from schedEndDisplayValue
    var schedEnd = new GlideDateTime();
    schedEnd.setDisplayValue(schedEndDisplayValue);

    // Pull the users local date from reqStart
    var startDate = reqStart.getDisplayValue().split(' ')[0];
    // Pull the users local time from schedStart
    var startTime = schedStart.getUserFormattedLocalTime();

    // Build a GDT composed of reqStart.date and schedStart.time
    var start = new GlideDateTime();
    start.setDisplayValue(startDate + ' ' + startTime);

    // Calculate the end date for the schedule by adding the difference between schedStart and schedEnd end to 'start'
    var end = new GlideDateTime();
    end.setDisplayValue(start.getDisplayValue());
    var diff = gs.dateDiff(schedStart, schedEnd, true); // Difference in seconds
    end.addSeconds(diff);

   // This code will return the correct dates for the 4am example, but will return incorrect dates for the 11pm request and other non-overnights
    // if (schedStart.getLocalDate() < schedEnd.getLocalDate()) {
    //     gs.info('Decrementing dates...');
    //     start.addDays(-1);
    //     end.addDays(-1);
    //     gs.info('Dates decremented.');
    // }

    gs.info(start.getDisplayValue() + ' - ' + end.getDisplayValue());

    return { "start": start, "end": end };
}

 

 

0 REPLIES 0