Exclude weekend from "Incident.Age.Days"

tsoct
Tera Guru

Hi Guys, I am pretty new to PA and very new to scripting. I am looking to exclude weekend from the formula indicator.

I guess I have to include GlideSchedule in the script but how?

Current OOB script:

var diff=function(x,y){return y.dateNumericValue() - x.dateNumericValue();};

var days=function(x,y){return diff(x,y)/(24*60*60*1000);};

days(current.opened_at, score_end);

1 ACCEPTED SOLUTION

Arnoud Kooi
ServiceNow Employee
ServiceNow Employee

The indicator is probably for triggering action on aging tickets.


I would not recommend looking at schedules, as things might get complex and hard to maintain.




Below script should exclude saturdays and sundays from the daycount.


Please try and let me know if it works



var diff=function(x,y){return y.dateNumericValue() - x.dateNumericValue();};


var businessDays = function(x,y){


  var xx = new GlideDateTime(x);  


  var startDayOfWeek = xx.getDayOfWeek(); //11 = monday 7 = sunday


  var ageDays = Math.floor(diff(x,y)/(24*60*60*1000)) || 0; //total day age


  var withoutFullWeekendDays = ageDays - (Math.floor(ageDays / 7) *2); //for full weeks substract 2 days per week


  var modDays = ageDays % 7; //remaining non full week days


  var substractDays = Math.min(Math.max(0,(startDayOfWeek - 5) + modDays),2); // should work, between w2 days substract


  return ( withoutFullWeekendDays - substractDays ) || 0;


};


businessDays(current.opened_at, score_end);


View solution in original post

11 REPLIES 11

Hi arnoud ,



Just found my indicator is incorrect where the summed age for resolved tickets shows -2 despite the collected jobs are correct...


find_real_file.png


I did some changes on the script by replacing score_end with current.resolved_at.


Could this be the reason of causing error above?



I am actually trying to find the sum days of resolved tickets.



The script:


find_real_file.png



The indicator:


find_real_file.png



Thanks.


How can you validate the data if this script has excluded weekends from the counts? 


If my response helped you, please click on "Accept as solution" and mark it as helpful.
- Saloni

Hi Arnoud,

 

Even after trying the above script I am still getting the age of incidents for all the days without excluding weekends. Could you let me know what might be the issue or is there any other way to achieve this?

 

Thanks!

I assume you would like to exclude holidays and any other non-working day, too?

Nope. I want to exclude only Weekends - Saturday and Sunday