Due Date based on schedule and duration from SLA

Brian Lancaster
Tera Sage

I trying to set the due date based on the schedule and duration from the SLA on the RITM. I'm doing via a script because we have a log of generic workflow that just create 1 task and there are several different SLAs. Also just like the SLA is setup to start only after the RITM has been approved. The problem I am facing is that it is not following the schedule fully. My schedule is Monday - Friday from 8 - 5. I have an SLA setup is a 9 hour duration or 1 business day. When my script run it is setting the end time at 7 am the next day but 7 am is not in the schedule. Is there something I'm doing wrong. Here is my script include I'm passing the sys_id of the SLA that is on that catalog item.

var setDueDate = Class.create();
setDueDate.prototype = {
    initialize: function() {},
    CalculateDueDate: function(SLA) {
        var gr = new GlideRecord('contract_sla');
	var dur = "";
        gr.addQuery('sys_id', SLA);
        gr.query();
        if (gr.next()) {
            var temp = gr.duration.getDisplayValue().toString().split(' ');
			if (temp.indexOf('Days') > -1 || temp.indexOf('Day') > -1){
				dur = temp[0] + " " + temp[2] + ":00:00";
			}
			else {
				dur = temp[0] + ":00:00";
			}
            var startDate = new GlideDateTime(gs.notDateTime());
            var fullDur = new GlideDuration(dur);
            var schedule = new GlideSchedule(gr.schedule, 'US/Pacific');
            var end = schedule.add(startDate, fullDur);
            return end;
        }
    },

    type: 'setDueDate'
};
1 ACCEPTED SOLUTION

DrewW
Mega Sage
Mega Sage

Ok so I ran this as a background script using one of our SLA's that is three days 8 to 5 except weekends and as long as I left out the time zone when setting up the schedule var it works correctly.  So I think you need to do the fix above and then remove the time zone.  Once you set the due date it will automatically get displayed in the users time zone and I think thats whats throwing it off.  There are two time zone offsets being included in the code you started with.

var setDueDate = Class.create();
setDueDate.prototype = {
    initialize: function() {},
    type: 'setDueDate'
};

setDueDate.CalculateDueDate = function(SLA) {
    var gr = new GlideRecord('contract_sla');
    if (gr.get(SLA)) {
        var startDate = new GlideDateTime();

gs.print(startDate.getDisplayValue());

        var fullDur = gr.duration.getGlideObject()
        var schedule = new GlideSchedule(gr.schedule);
        var end = schedule.add(startDate, fullDur);
        return end;
    }
}

gs.print((setDueDate.CalculateDueDate("270ef00cd7133200bbc783e80e6103f1")).getDisplayValue());

View solution in original post

13 REPLIES 13

DrewW
Mega Sage
Mega Sage

You have a typo.  gs.notDateTime should be gs.nowDateTime

I would do it like this and it should work but did not test it.

var setDueDate = Class.create();
setDueDate.prototype = {
    initialize: function() {},
    type: 'setDueDate'
};

setDueDate.CalculateDueDate = function(SLA) {
    var gr = new GlideRecord('contract_sla');
    if (gr.get(SLA)) {
        var startDate = new GlideDateTime(gs.nowDateTime());
        var fullDur = gr.duration.getGlideObject()
        var schedule = new GlideSchedule(gr.schedule, 'US/Pacific');
        var end = schedule.add(startDate, fullDur);
        return end;
    }
}

 

Both with fixing my typo and trying your script I'm still facing the issue where it is ignoring the 8 - 5 time frame and setting the due date to the 7 AM hour.

Have you verified your schedule to make sure its setup correctly?

Yes because the SLAs are working as excepted. They only calculate during the 8 - 5 business hours.