Exclude Weekends and Holidays for Calculate TTR

Niranjan2
Tera Contributor

Hi,
Iam looking to create a Report which track Time to Resolve Tickets, for this I'm in the process of configuring an Action in flow designer, we already have an action available which calculate the TTR. Looking to exclude Weekends and Hoildays for that. having issue in coding part within the Action. Any help is appreciated.
Attached the current script.

 

 

 

@Ankur Bawiskar @Jaspal Singh 

14 REPLIES 14

Jitendra Diwak1
Kilo Sage

Hi @Niranjan2,

 

You could try this below code:

 

(function() {
    // Define holidays (populate this array with actual holiday dates)
    var holidays = ["2024-01-01", "2024-12-25"]; // Example: New Year's Day, Christmas

    // Function to check if a given date is a weekend
    function isWeekend(date) {
        var day = date.getDay();
        return (day === 0 || day === 6); // 0 is Sunday, 6 is Saturday
    }

    // Function to check if a given date is a holiday
    function isHoliday(date) {
        var dateString = date.toISOString().split('T')[0]; // Get YYYY-MM-DD format
        return holidays.includes(dateString);
    }

    // Function to calculate business days between two dates excluding weekends and holidays
    function calculateBusinessDays(startDate, endDate) {
        var days = 0;
        var currentDate = new Date(startDate);
        while (currentDate <= endDate) {
            if (!isWeekend(currentDate) && !isHoliday(currentDate)) {
                days++;
            }
            currentDate.setDate(currentDate.getDate() + 1);
        }
        return days;
    }

    // Sample usage
    var openedDateTime = new Date("2024-05-20T08:00:00Z"); // Example: Ticket opened date and time
    var resolvedDateTime = new Date("2024-05-25T16:00:00Z"); // Example: Ticket resolved date and time
    var businessDays = calculateBusinessDays(openedDateTime, resolvedDateTime);
    gs.info("Time to Resolve (TTR): " + businessDays + " business days");
})();

 

Please accept my solution if it works for you.

 

Thanks

Jitendra

Please accept my solution if it works for and thumps up.

@Jitendra Diwak1 Thanks for the response, can we use the same script in Flow designer Action script? As we have Inputs and Outputs. also, currently i don't have a list of holidays. so, trying to make use of GlideSchedule.

Rajesh_Bhise
Tera Guru

Hello @Niranjan2 ,

 

Below flow action script would help you to exclude weekends using OOTB GlideDateTime API.

 

(function execute(inputs, outputs) {
 
  var createdOn = new GlideDateTime(inputs.Created);
  var createdDay = createdOn.getDayOfWeekLocalTime();   // getDayOfWeekLocalTime() - Gets the day of the week stored by the GlideDateTime object, expressed in the user's time zone.
  var resolvedAt = new GlideDateTime(inputs.Resolved);
  var resolvedDay = resolvedAt.getDayOfWeekLocalTime();
  
  if((createdDay == '6'|| createdDay == '7') || (resolvedDay == '6'|| resolvedDay == '7')){  // 6-Saturday & 7-Sunday
      var ttr = '';
  }else{
  var ttr = GlideDateTime.subtract(createdOn, resolvedAt).getDurationValue();
  outputs.time_to_resolve=ttr;
  }
})(inputs, outputs);
 
Please mark this answer to correct and helpful if your query is resolved or some help you gained from my post.
 
Thank You,
Rajesh

Hi @Rajesh_Bhise, Thanks for the reply. I'm looking to Exclude Weekends and Holidays for Calculate TTR.

Hello @Niranjan2 ,

 

Below is the script that considers weekends and Holidays to calculate TTR.

 

(function execute(inputs, outputs) {

var holidays = ["2024-01-01", "2024-12-25"]; // Example: New Year's Day, Christmas

var createdOn = new GlideDateTime(inputs.Created);

var createdDate = new GlideDate(inputs.Created);
var createdDay = createdOn.getDayOfWeekLocalTime(); // getDayOfWeekLocalTime() - Gets the day of the week stored by the GlideDateTime object, expressed in the user's time zone.
var resolvedAt = new GlideDateTime(inputs.Resolved);

var resolvedDate = new GlideDate(inputs.Resolved);
var resolvedDay = resolvedAt.getDayOfWeekLocalTime();

function isHoliday(date) {
var dateString = date.toISOString().split('T')[0]; // Get YYYY-MM-DD format
return holidays.includes(dateString);
}

if(((createdDay == '6'|| createdDay == '7') || (resolvedDay == '6'|| resolvedDay == '7')) || (isHoliday(createdDate)) || (isHoliday(resolvedDate))){ // 6-Saturday & 7-Sunday
var ttr = '';
}else{
var ttr = GlideDateTime.subtract(createdOn, resolvedAt).getDurationValue();
outputs.time_to_resolve=ttr;
}
})(inputs, outputs);

 

Please mark this answer to correct and helpful if your query is resolved or some help you gained from my post.

Happy to help you in future.

 

Thank you,

Rajesh