Exclude weekends in duration field

TtharunS
Tera Contributor

We have an SLA based on the ‘First Response Duration’ field. When a user from a specific group comments for the first time, that duration is captured in the SLA field. We need to exclude weekends from this duration. We have already developed a Business Rule to populate the duration (see script below). Could you please suggest how to exclude weekends?

(function executeRule(current, previous /*null when async*/ ) {

    //Getting sysId of the group "Clara Technology Team" from the system property 'x_snc_coe.QSCAssignmentGroup'
    var QSCAssignmentGroups = gs.getProperty('x_snc_coe.QSCAssignmentGroup');
    var QSCAssignmentGroupsObj = JSON.parse(QSCAssignmentGroups);
    var cttGroupSysID = QSCAssignmentGroupsObj["Clara Technology Team"];

    var hwQSC = new sn_hw.HistoryWalker(current.getTableName(), current.getUniqueValue());
    hwQSC.walkTo(0);
    do {
        if (hwQSC.getWalkedRecord().assignment_group == cttGroupSysID) {
            var walkNumber = hwQSC.getUpdateNumber(); //getting the update number when the job was first reassigned to the group 'Clara Technology Team'
            break;
        }
    }
    while (hwQSC.walkForward());

    hwQSC.walkTo(walkNumber);
    var date = hwQSC.getWalkedRecord().sys_updated_on;
    var reassignedTime = new GlideDateTime(date);
    var nowTime = new GlideDateTime();

    var duration = GlideDateTime.subtract(reassignedTime, nowTime);
    current.sla_on_first_response_ctt = duration;

})(current, previous);

 

1 ACCEPTED SOLUTION

joshuajacks
Tera Guru

First make sure the schedule record you are using has the correct timeframe and is excluding the weekends. Next use the "schedule.duration()" method. It looks like you tried "schedule.subtract" instead.

View solution in original post

4 REPLIES 4

joshuajacks
Tera Guru

Hey @TtharunS  You can create a Schedule (cmn_schedule) record that excludes the weekend. Then use the GlideSchedule api to calculate the duration between the two dates. The duration method will do the calculation for you.
This is the code snippet from the documentation:

var startDate = new GlideDateTime('2014-10-16 02:00:00');
var endDate = new GlideDateTime('2014-10-18 04:00:00');
var schedule = new GlideSchedule();
 
schedule.load('090eecae0a0a0b260077e1dfa71da828'); // loads "8-5 weekdays excluding holidays" schedule
var duration = schedule.duration(startDate, endDate);
gs.info(duration.getDurationValue()); // gets the elapsed time in schedule

GlideSchedule | ServiceNow Developers

I tried the script below. When I add the first comment, the duration is not getting populated in the SLA First Response field. Could you please suggest how to achieve this?

var date = hwQSC.getWalkedRecord().sys_updated_on;
    var reassignedTime = new GlideDateTime(date);
    var nowTime = new GlideDateTime();
    var schedule = new GlideSchedule();
    schedule.load('fdd435561b2274907707fc4cd34bcb8a');
    r duration = schedule.subtract(reassignedTime, nowTime);
    current.sla_on_first_response_ctt = duration;


I tried the script below. When I add the first comment, the duration is not getting populated in the SLA First Response field. Could you please suggest how to achieve this?
 var date = hwQSC.getWalkedRecord().sys_updated_on;
    var reassignedTime = new GlideDateTime(date);
    var nowTime = new GlideDateTime();
    var schedule = new GlideSchedule();
    schedule.load('fdd435561b2274907707fc4cd34bcb8a');
    var duration = GlideDateTime.subtract(reassignedTime, nowTime);
    current.sla_on_first_response_ctt = duration;

joshuajacks
Tera Guru

First make sure the schedule record you are using has the correct timeframe and is excluding the weekends. Next use the "schedule.duration()" method. It looks like you tried "schedule.subtract" instead.