Hi,
So I have two solutions that a friend and I (mostly him) were able to come up with, but while testing it we realized that while the test environment seemed to be executed properly, the dashboard indicator said "Unable to execute Arcade script". We performed some process of elimination and discovered that our custom functions seemed to be working in the test environment, but failing in the display widget for the dashboard. Below is the code that failed. After that snippet is the final code rewritten to not use functions (and it displayed properly). The two codes main purpose is to get that date in the future accounting for weekends and holidays, but the second code is modified to show other relevant data.
Does anyone know why the first code, which is cleaner and passed the environment test failed to display in the widget?
var reference = 2.34
var daystofinish = $datapoint["count_ObjectID"]/reference;
function addworkdays (startdate , num_of_working_days) {
var date = startdate;
for (var i=0; i < num_of_working_days; i++) {
date = dateadd(date,1,'days');
date = avoidweekends(date);
}
return date;
}
function avoidweekends (funcdate) {
var holidays =
[ Date(2022, 1, 2), // New Year's Day
Date(2022, 12, 31), // New Year's Day
Date(2022, 0, 17), // MLK Day
Date(2022, 0, 18), // Susan B Anthony Day
Date(2022, 0, 19), // Memorial Day
Date(2022, 5, 20), // Juneteeth Day
Date(2022, 7, 4), // Independence Day
Date(2022, 9, 5), // Labor Day
Date(2022, 10, 10), // Columbus Day
Date(2022, 11, 11), // Veteran's Day
Date(2022, 11, 24), // Thanksgiving Day
Date(2022, 12, 26), // Christmas Day
]
var sunday = 0;
var saturday = 6;
if (weekday(funcdate) == sunday || weekday(funcdate) ==saturday || Includes(holidays, funcdate)) {
funcdate = dateadd(funcdate, 1,'days');
return avoidweekends(funcdate);
}else{
return funcdate;
}
}
var outdate = addworkdays(today(), (daystofinish));
return {
middleText: outdate
}
//this code passed the test environment but failed in the widget as mentioned above.
Once the functions were eliminated, accounting for predictable holidays was done with some messier coding, but it appears to be working properly. See below for the solution that worked in the widget.
var holidays =
[ Date(2021, 11, 31), // New Year's Day
Date(2022, 0, 17), // MLK Day
Date(2022, 1, 21), // presday
Date(2022, 4, 30), // Memorial Day
Date(2022, 5, 20), // Juneteeth Day
Date(2022, 6, 4), // Independence Day
Date(2022, 8, 5), // Labor Day
Date(2022, 9, 10), // Columbus Day
Date(2022, 10, 11), // Veteran's Day
Date(2022, 10, 24), // Thanksgiving Day
Date(2022, 10, 25), // day after Thanksgiving Day
Date(2022, 11, 26), // Christmas Day
]
var sunday = 0;
var saturday = 6;
var currentday = today();
var sitesleft = $datapoint["count_ObjectID"]
var rate = 2.34
var days_to_add = ceil(sitesleft/rate);
var calculated_date = currentday;
for (var i=0; i < days_to_add; i++) {
calculated_date = dateadd(calculated_date,1,'days');
if (weekday(calculated_date) == saturday){
calculated_date = dateadd(calculated_date, 2,'days')
}
if (weekday(calculated_date) == sunday){
calculated_date = dateadd(calculated_date, 1,'days');
}
// at this point the calcdate should
//be a weekday but could still be a holiday
if (Includes(holidays, calculated_date)){
// checks if this new date is a holiday
if (weekday(calculated_date) == 4){
//checks if its a thursday holiday
if (Includes(holidays,dateadd(calculated_date,1,'days'))){
// followed by a fri holiday aka thanksgiving
calculated_date = dateadd(calculated_date, 4,'days');
//adds 4 days to thursday to get to monday
}else{
/* if its not thanksgiving then Fri is not a holiday,
so add 1 day to get to Fri*/
calculated_date = dateadd(calculated_date, 1,'days');
}
}
if (weekday(calculated_date) > 0 &&
weekday(calculated_date) < 4){
// if the holiday lands on a mon,tues,weds
calculated_date = dateadd(calculated_date, 1,'days');
}
if (weekday(calculated_date) == 5){
// if the holiday lands on a Fri
calculated_date = dateadd(calculated_date, 3,'days');
}
}
}
return {
topText: sitesleft+ ' inspections reports remaining* ',
topTextMaxSize: 'medium',
middleText: days_to_add+ '** inspection days remaining',
middleTextMaxSize: 'medium',
bottomText: text((calculated_date), 'M/D/Y')+ ' end date',
bottomTextMaxSize: 'Large',
bottomTextColor: IIf(calculated_date >= Date(2022,03,28), 'red',
'green')
}