You need to sign in to do that
Don't have an account?
Mthobisi Ndlovu 2
System.LimitException: Apex CPU time limit exceeded Error
Hi Guys.
I created a method that gets the number of weekend days between 2 dates.However when executing it, it throws a System.LimitException: Apex CPU time limit exceeded error. How can I solve this.. see code below. Thanks.
I created a method that gets the number of weekend days between 2 dates.However when executing it, it throws a System.LimitException: Apex CPU time limit exceeded error. How can I solve this.. see code below. Thanks.
public static integer getNumberOfWeekendDaysPerMonth(Datetime startDate, Datetime endDate){ integer i = 0; while (startDate < endDate) { if(startDate.format('E') == 'Sat' || startDate.format('E') == 'Sun') { // The line that throws the error. i += 1; } startDate.addDays(1); } return i; }
Just update on line and see your code will run. You are not assigning Startdate again in startdate
-Thanks
Ashlekh Gera
All Answers
Are you calling this method inside a for loop ?
No I'm not calling it inside a for loop, only calling it in one method to calculate the number of working days per month.
It throws the error even if I execute it anonymously.
Just update on line and see your code will run. You are not assigning Startdate again in startdate
-Thanks
Ashlekh Gera
@Manoj Start Date is the first day (date) of the month and End Date is last day(date) of the month, will add the code with AKGs suggestion.