1

I'm not sure what's happening, but all what I'm trying to do is to write a simple test class which inserting a lot of records. However, my records are related in different relations.


My test class is:

@isTest
private class A800ExpiredClientReportV2Test {

static testMethod void reportingExpiredClientTest() {
    Integer bulkDataValue = 200;
    Client__c randomClient = new Client__c(Name__c = 'MAZEN', Phone__c='0000');
    insert randomClient;

    // Targeting     ->    First SOQL statment, attribut name: quotList
    // SELECT ID, To__c, Quotation_Total__c, Time_Left__c, Client_Name__c, Client__c, Name, Phone__c, Confirm_Renewal__c 
    //       FROM Quotation__c WHERE 
    // (Expiry_Notification_1__c = true AND Confirm_Renewal__c = false AND Stored_Inventory_Items__c > 0 AND Paid_Invoices__c >0)
    //       OR 
    // (Expiry_Notification_1__c = true AND Subsequent_Quotation__c != null AND Paid_Invoices__c >0)
    //       ORDER BY To__c
    Date nowDay = Date.Today();
    List<Quotation__c> quotList_tst = new List<Quotation__c>();
    for(Integer i = 0; i < bulkDataValue; i++){
        if(math.mod(i, 2) == 0){
            // (Expiry_Notification_1__c = true AND Confirm_Renewal__c = false AND Stored_Inventory_Items__c > 0 AND Paid_Invoices__c > 0)
            quotList_tst.add(new Quotation__c(Client__c = randomClient.Id, Duration_Months__c = 1, From__c = nowDay.addDays(10), Confirm_Renewal__c = false));
        } else {
            // (Expiry_Notification_1__c = true AND Subsequent_Quotation__c != null AND Paid_Invoices__c >0)
            quotList_tst.add(new Quotation__c(Client__c = randomClient.Id, Duration_Months__c = 1, From__c = nowDay.addDays(10), Subsequent_Quotation__c = quotList_tst[i-1].Id));
        }
    }
    insert quotList_tst;

    // Process: Stored_Inventory_Items__c > 0
    List<Inventory_Sheet__c> quotList_tst_InventorySheet = new List<Inventory_Sheet__c>();
    for(Integer i = 0; i < bulkDataValue; i++){
        if(math.mod(i, 2) == 0){
            quotList_tst_InventorySheet.add(new Inventory_Sheet__c(Quotation__c = quotList_tst[i].Id));
        }
    }
    insert quotList_tst_InventorySheet;

    List<Inventory_Line_Item__c> quotList_tst_InventorySheet_InventeryItem = new List<Inventory_Line_Item__c>();
    for(Integer i = 0; i < bulkDataValue; i+2){
        //if(math.mod(i, 2) == 0){
        quotList_tst_InventorySheet_InventeryItem.add(new Inventory_Line_Item__c(Inventory_Sheet__c = quotList_tst_InventorySheet[i].Id, Code_Number__c = 'any Code', Entry_Date__c = Date.newInstance(2016,12,16))); // Line #41
        //}
    }
    insert quotList_tst_InventorySheet_InventeryItem;
    // END Process


    // Process: Paid_Invoices__c > 0
    List<Invoice__c> paidInvoicesFor_quotList_tst = new List<Invoice__c>();
    for(Integer i = 0; i < bulkDataValue; i++){
        paidInvoicesFor_quotList_tst.add(new Invoice__c(Quotation__c = quotList_tst[i].Id, Status__c = 'Paid', Method_of_Payment__c = 'Cash', Payment_Date__c = Date.Today()));
    }
    insert paidInvoicesFor_quotList_tst;
    // END Process

    Test.startTest();
        A800ExpiredClientReportV2 expiredClientReport = new A800ExpiredClientReportV2();
    Test.stopTest();

    Set<Id> expectedRecordIds_quotList = new Map<Id, SObject>(quotList_tst).keySet();
    Set<Id> actualIds_quotList = new Map<Id, SObject>(expiredClientReport.quotList).keySet();
    system.assert(expectedRecordIds_quotList.size() == bulkDataValue);
    system.assert(actualIds_quotList.size() == bulkDataValue);
    system.assertEquals(expectedRecordIds_quotList, actualIds_quotList);
}
}

What I'm trying to do is: Inserting one Inventory_Line_Item__c record, so its Inventory_Sheet__c parent record updates its Stored_Items__c field [Roll-Up Summary (COUNT Inventory Item)], so the field Stored_Inventory_Items__c in Quotation__c object will have a value of more than 0, which what I need for my SOQL statement.
When I click run, the following error message coming up:

System.LimitException: Apex CPU time limit exceeded Class.A800ExpiredClientReportV2Test.reportingExpiredClientTest: line 41, column 1

I referred to Line 41 as a comment in my code.
I looked up about this error, and generally all answers talking about nested loops, but I'm not sure if there are any nested loops in my code behind the scene. :/

7

Your loop:

for(Integer i = 0; i < bulkDataValue; i+2){

never terminates hence you hit the CPU limit.

You probably meant something like:

for(Integer i = 0; i < bulkDataValue; i += 2){
  • Fine catch there! I was going to wax philosophical about checking for inefficient and tightly interwoven triggers, and moving repeated function calls like nowDate.addDays(10) outside of loops. – Derek F Jul 7 '16 at 13:56
  • @DerekF Yeah, rare to see a case where a governor limit is doing 100% good. My first thought was bad trigger logic. – Keith C Jul 7 '16 at 13:58
  • Now, it's giving me this: System.ListException: List index out of bounds: 100 Class.A800ExpiredClientReportV2Test.reportingExpiredClientTest: line 41, column 1. It seems that this loop never stops :/ !! – MAZux Jul 7 '16 at 16:04
  • I even tried to leave it for(Integer i = 0; i < bulkDataValue; i ++){ and un-comment the if(math.mod(i, 2) == 0) statement below it, but same error. @KeithC – MAZux Jul 7 '16 at 16:08
  • 3
    @MAZux See How do I start to debug my own Apex code?. – Keith C Jul 7 '16 at 16:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.