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

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);


Have you tried if this solution works for you?


Work like a charm! Thanks for the solution. Much appreciated


Hello,

I have tried the above solution but unable to get the records displayed in the widget of the dashboard. Since you have mentioned that the above solution worked for you, could you please post the detailed execution of that solution along with the screenshots and how and where(like in indicator or breakdown etc..)you have used the above script and was able to get the records based upon their age excluding weekends.

 

Thanks and Regards,

Nasreen