How to evaluate Timer Script

1
0
-1

Hello Community
I have a timer script on each human Task. I need to check if day of the week is Sunday to Thursday. Weekends are Friday and Saturday.

If its weekday then timer should execute at 24 hours. Else if its Friday or Saturday then timer should execute on Sunday. i have the below script.
But I still keep getting escalations on friday and saturday.Please suggest where is my script going wrong.

============================================================================================

Calendar cal = Calendar.getInstance();

def dayOfWeek = cal.get(Calendar.DAY_OF_WEEK);

if(dayOfWeek == 5){

return 259200000L;
}
else if (dayOfWeek == 6){

return 172800000L;
}
else
{
return 86400000L;
}

===============================================================

Regards

1 answer

1
0
-1
This one is the BEST answer!

Hi, I recommend using the java.time api to compute this and use a fixed date calculation instead of a duration:

import java.time.DayOfWeek
import java.time.LocalDate
import java.time.LocalDateTime
import java.time.ZoneId
import java.time.ZoneOffset
import java.time.temporal.TemporalAdjusters
import java.time.temporal.TemporalAmount

def weekdays = [DayOfWeek.SUNDAY, DayOfWeek.MONDAY, DayOfWeek.TUESDAY, DayOfWeek.WEDNESDAY]
def today = LocalDateTime.now()
if(weekdays.contains(today.getDayOfWeek())){
        // Return in 24h
         return Date.from(today.plusDays(1).atZone(ZoneId.systemDefault()).toInstant());
}else{
        // Return next sunday
        return Date.from(today.with(TemporalAdjusters.next(DayOfWeek.SUNDAY)).atZone(ZoneId.systemDefault()).toInstant());
}

NB: I have removed Thrurday from weekdays because I guess that you want the timer to be triggered on Sunday and not on Friday when the task is instantiated a Thursday.

NB2: Becareful with the timezone management (if your users have a different timezone than the server), you may want to return a fixed hour in the day to get rid of this issue.

Comments

Submitted by Dibyajit.Roy on Tue, 02/23/2021 - 15:18

Hello Romain

Thank you for your answer. I tried your code in my Local System and I see correct output.
I will Test this code for different days of the week and finally upload the same code in Production for all users.

Our servers and users are in the same timezone. I hope I will not face any problems related to timezone.

Regards

Notifications