+ Start a Discussion
Mthobisi Ndlovu 2Mthobisi 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.
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;
    }

 
Best Answer chosen by Mthobisi Ndlovu 2
AshlekhAshlekh
Hi,

Just update on line and see your code will run. You are not assigning Startdate again in startdate
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 =   startDate.addDays(1);
        }
        return i;
    }

-Thanks
Ashlekh Gera
 

All Answers

ManojjenaManojjena
Hi   Mthobisi Ndlovu ,
Are you calling this method inside a for loop ?

 
Mthobisi Ndlovu 2Mthobisi Ndlovu 2
Hey Manoj.

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.
integer numWeekends = getNumberOfWeekendDaysPerMonth((Datetime)startDate, (Datetime)endDate );

It throws the error even if I execute it anonymously.
AshlekhAshlekh
Hi,

Just update on line and see your code will run. You are not assigning Startdate again in startdate
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 =   startDate.addDays(1);
        }
        return i;
    }

-Thanks
Ashlekh Gera
 
This was selected as the best answer
Mthobisi Ndlovu 2Mthobisi Ndlovu 2
@AKG thanks it works fine now.. totally missed that.

@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.
public static integer getNumberOfWorkingDaysPerMonth(Date dt) {
        integer numWorkingDays;
        Date startDate = dt.toStartOfMonth();
		Date endDate = startDate.addMonths(1) - 1;
		integer totalNumberOfDays = DateUtilities.numOfDaysPerMonth(dt); 
        integer numWeekends = getNumberOfWeekendDaysPerMonth((Datetime)startDate, (Datetime)endDate );
        
        numWorkingDays = totalNumberOfDays - numWeekends;
        return numWorkingDays;
    }
    
    public static integer getNumberOfWeekendDaysPerMonth(Datetime startDate, Datetime endDate){
        
        integer i = 0;
        while (startDate < endDate) {
            if(startDate.format('E') == 'Sat' | startDate.format('E') == 'Sun') {
                i += 1;
            }
           startDate = startDate.addDays(1);
        }
        return i;
    }

 
Mthobisi Ndlovu 2Mthobisi Ndlovu 2
Correction on the While loop condition it should be: (startDate <= endDate)