+ Start a Discussion

Test Method SoQL limit using cumulative count instead of per DML operation

I have a number of classes and triggers with test methods that have all been functioning correctly for months.  There were certain (rare) cases where the SoQL Query limit was being hit, so to avoid this, I annotated one method as @future.  I then run the tests, and even though it's the exact same code as before just plus the @future, it's now returning "Too many SoQL queries" as an error.  In going through the debug logs, no single DML operation uses more than 20 queries (not even close), but cumulatively the inserts in the test method are hitting 21.  The cumulative limit for SoQL queries is supposed to be 100, not 20?


I can't possibly post all of the code behind this, but this is the test method itself.  The only thing that's changed from when it was working is that one of the methods called as a result is now @future and adding the startTest / stopTest - could that have an impact?  It hits the SoQL limit error on the last Event insert.  Anyone have any ideas?



static testMethod void testAdjournedCourtDateCheck() { Test.startTest(); Event event1; // Find the ID of the CEP Account Account CEP = [SELECT ID FROM Account WHERE Account.Name = 'CEP' and Account.RecordType.Name = 'CASES Programs']; // Create some sample dates for insert commands Date birthDate1 = Date.newInstance(1976,11,20); Date activityDate = Date.newInstance(2006,11,20); Datetime activityDatetime = Datetime.newInstance(2006,11,20); Date activityDate2 = Date.newInstance(2006,11,21); Datetime activityDatetime2 = Datetime.newInstance(2006,11,21); Date referDate1 = Date.newInstance(2006,11,20); Date intakeDate1 = Date.newInstance(2006,11,21); // Set the RecordTypes Id eventType = '012600000004yjw'; Id contactType = '012600000004zC5'; Id intakeType = '012600000004z70'; //Create a Test Client Contact testContact = new Contact(FirstName = 'Unit', LastName = 'Tester', AccountId = CEP.Id, Gender__c = 'Male', Birthdate = birthDate1, RecordTypeID = contactType); insert testContact; //Insert an Intake case for the client Case_Information__c cinfos_intake = new Case_Information__c(Client__c = testContact.Id, Name = 'test777777', Borough__c = 'X - Bronx', Referred_On__c = referDate1, Intake_Date__c = intakeDate1, RecordTypeID = intakeType); insert cinfos_intake; //Insert an adjourned Court Date Appointment Boolean IsError1 = false; try{ event1 = new Event( Outcome__c = 'Adjourned', Subject = 'Court Date', ActivityDate = activityDate, ActivityDateTime = activityDatetime, RecordTypeID = eventType, DurationInMinutes = 60, WhatId = cinfos_intake.Id ); insert event1; } catch(Exception e) { IsError1 = true; System.debug('The error is: ' +e); } //Insert another Court Date Appointment Boolean IsError2 = false; try{ event1 = new Event( Outcome__c = 'Pending', Subject = 'Court Date', ActivityDate = activityDate2, ActivityDateTime = activityDatetime2, RecordTypeID = eventType, DurationInMinutes = 60, WhatId = cinfos_intake.Id ); insert event1; } catch(Exception e) { IsError2 = true; } Test.stopTest(); //First event insert should go in System.assertEquals(false, IsError1); //Make sure Disposition Date being updated cinfos_intake = [Select Id, Court_Dates_After_Adjourned_Outcome__c from Case_Information__c where Id = :cinfos_intake.Id]; System.assertEquals(true, cinfos_intake.Court_Dates_After_Adjourned_Outcome__c); }//end testAdjournedCourtDateCheck




Best Answer chosen by Admin (Salesforce Developers) 
I've answered my own question, it is the startTest method that did it.  All I had to do was move that startTest method down after I set up all the test data and it was fine.  It's still limited to 20 queries inide of the start/stop, but fortunately that's not an issue for me. If anyone else gets confused about this like I did, check here- http://wiki.apexdevnet.com/index.php/An_Introduction_to_Apex_Code_Test_Methods
Message Edited by jgrenfell on 03-01-2009 10:06 AM