+ Start a Discussion
Adam RycroftAdam Rycroft 

Help with: System.LimitException: Apex CPU time limit exceeded

Hello, I'm getting the above error message in a test class. Stack Trace:
Class.testProcessIncome.createClientAndHouseholdAndIncome: line 58, column 1
Class.testProcessIncome.testBulkIncome: line 211, column 1
 
@isTest
public class testProcessIncome {
    
    public static void createClientAndHouseholdAndIncome (integer numofClients, integer numOfHousehold, integer numOfIncome){
        
        list<account> aList = new list<account>();
        
        for(integer i = 1; i <= numOfClients; i++){
            account newA = new account(
                firstname = 'Test',
                lastname = 'Client',
                social_security_number__c = '000-00-0000',
                personBirthdate = date.newInstance(1980, 1, 1),
                gender__c = 'Female',
                ethnicity__c = 'Client refused',
                language__c = 'English',
                hiv_status__c = 'AIDS',
                secondary_disability__c = 'No Secondary Disability'
            );
            aList.add(newA);
        }
     
      insert aList;
        
        list<household_member__c> HouseholdList = new list<household_member__c>();
        
        for(account a : aList){
            for(integer i = 1; i <= numOfHousehold; i++){
                
                if(i<=12){
                    household_member__c h = new household_member__c(
                        status__c = 'Current',
                        household__c = a.id
                    );                    
                  HouseholdList.add(h);
                    
                } else {
                    household_member__c h = new household_member__c(
                        status__c = 'Current',
                        household__c = a.id
                    );                
                    HouseholdList.add(h);
                }
            }
        }
        
        insert HouseholdList;
        
        list<household_member__c> hCheck = [select id, household__c, status__c from household_member__c];
        system.debug(hCheck);
        
        list<income__c> IncomeList = new list<income__c>();
        
        for(household_member__c h : HouseholdList){
            for(account a : aList){
                for(integer i = 1; i <= numOfIncome; i++){
                    if(i<=12){
                        income__c d = new income__c(
                            start_of_pay_period__c = date.newInstance(2017, i, 1),
                            Household_Member__c = h.id,
                            Client__c = a.id
                        );                    
                      IncomeList.add(d);
                        
                    } else {
                        income__c d = new income__c(
                            start_of_pay_period__c = date.newInstance(2017, 1, 1),
                            household_member__c = h.id,
                            Client__c = a.id
                        );                
                        IncomeList.add(d);
                    }
                }
            }
         }
        
        insert IncomeList;
        
        list<income__c> dCheck = [select id, household_member__c, client__c, status__c, start_of_pay_period__c from income__c];
        system.debug(dCheck);
        
  }
    
    public static testMethod void insertincome(){
        
        createClientAndHouseholdandIncome(1,2,3);
        
        account a = [select id from account limit 1];
        household_member__c h = [select id from household_member__c limit 1];
        
        income__c d = new income__c(
          start_of_pay_period__c = date.newInstance(2017, 02, 15),
          household_member__c = h.id,
          client__c = a.id
        );
        
        test.startTest();
        
        insert d;
        
        test.stopTest();
        
        income__c dUpdated = [select id, status__c, client__c from income__c where start_of_pay_period__c = 2017-02-15];
        
        system.assertEquals('Current',dUpdated.status__c);
        
    }
    
    public static testMethod void insertMultipleIncome(){
        
        test.startTest();
        
        createClientAndHouseholdAndIncome(1,2,3);
        
        test.stopTest();
        
        household_member__c h = [select id, (select id, status__c, client__c, start_of_pay_period__c from Incomes__r order by start_of_pay_period__c desc, createddate desc) from household_member__c limit 1];
        
        for(income__c d : h.incomes__r){
            
            if(d.id == h.incomes__r[0].id){
                system.assertEquals('Current',d.status__c);
            } else {
                system.assertEquals('Past',d.status__c);
            }
            
        }
        
    }
    
    public static testMethod void deleteIncome(){
        
        createClientAndHouseholdAndIncome(1,2,3);
        
        household_member__c h = [select id, (select id, status__c, client__c, start_of_pay_period__c from incomes__r order by start_of_pay_period__c desc, createddate desc) from household_member__c limit 1];
        
        list<income__c> dListToDelete = new list<income__c>();
        
        income__c dToDelete = [select id from income__c where status__c = 'Current' limit 1];
        
        test.startTest();
        
        delete dToDelete;
        
        for(household_member__c hUpdated : [select id, (select id, status__c, client__c, start_of_pay_period__c from incomes__r order by start_of_pay_period__c desc, createddate desc) from household_member__c where id = :h.id]){
            system.assertEquals(0,hUpdated.incomes__r.size());
            for(income__c d : hUpdated.incomes__r){
                system.assertEquals('Current',d.status__c);
            }
        }
        
        income__c dToUndelete = [select id from income__c where isdeleted = true limit 1 ALL ROWS];
        
        undelete dToUndelete;
        
        for(household_member__c hUpdated : [select id, (select id, status__c, client__c, start_of_pay_period__c from incomes__r order by start_of_pay_period__c desc, createddate desc) from household_member__c where id = :h.id]){
            system.assertEquals(0,hUpdated.incomes__r.size());
            for(income__c d : hUpdated.incomes__r){
                if(d.id == hUpdated.incomes__r[0].id){
                  system.assertEquals('Current',d.status__c);
                } else {
                    system.assertEquals('Past',d.status__c);
                }
            }
        }
        
        test.stopTest();
        
    }
    
    public static testMethod void updateIncomeDate(){
        
        createClientAndHouseholdAndIncome(1,2,3);
        
        household_member__c h = [select id, (select id, status__c, client__c, start_of_pay_period__c from incomes__r order by start_of_pay_period__c desc, createddate desc) from household_member__c limit 1];
        
        list<income__c> dList = new list<income__c>();
        
        for(income__c d : h.incomes__r){
            if(d.id == h.incomes__r[0].id){
                d.start_of_pay_period__c = date.newInstance(2016,12,1);
                dList.add(d);
            }
        }
        
        test.startTest();
        
        checkRecursive.reset();
        update dList;
        
        test.stopTest();
        
        household_member__c hUpdated = [select id, (select id, status__c, client__c, start_of_pay_period__c from incomes__r order by start_of_pay_period__c desc, createddate desc) from household_member__c limit 1];
        
        list<income__c> dListUpdated = new list<income__c>();
        
        for(income__c d : hUpdated.incomes__r){
            
            if(d.id == hUpdated.incomes__r[0].id){
                system.assertEquals('Current',d.Status__c);
                system.assertEquals(date.newInstance(2017,1,1),d.start_of_pay_period__c);
            } else {
                system.assertEquals('Past',d.Status__c);
            }
        }
        
    }
    
    public static testMethod void testBulkIncome(){
        
        createClientAndHouseholdAndIncome(200,10,10);
        
        list<income__c> dListToModify = new list<income__c>();
        
        for(income__c d : [select id, start_of_pay_period__c, status__c from income__c where status__c = 'Current']){
            system.assertEquals(date.newInstance(2017,10,1),d.start_of_pay_period__c);
            income__c dToUpdate = new income__c(
                id = d.id,
              start_of_pay_period__c = date.newInstance(2017,8,15)
            );
            dListToModify.add(dToUpdate);
        }
        
        checkRecursive.reset();
        update dListToModify;
        
        list<income__c> dListToDelete = new list<income__c>();
        
        for(income__c d : [select id, start_of_pay_period__c, status__c from income__c where status__c = 'Current']){
            system.assertEquals(date.newInstance(2017,9,1),d.start_of_pay_period__c);
            dListToDelete.add(d);
        }
        
        delete dListToDelete;
        
        for(income__c d : [select id, start_of_pay_period__c, status__c from income__c where status__c = 'Current']){
            system.assertEquals(date.newInstance(2017,8,15),d.start_of_pay_period__c);
        }
        
    }

}

Any suggestions on how to fix?