+ Start a Discussion

Too many Query Rows, in test method

While updating a test method I received the error mentioned in this subject. I am not sure where the offendor lies so can anyone assist, I have attached the code to give a better picture.

 * This class contains unit tests for validating the behavior of Apex classes
 * and triggers.
 * Unit tests are class methods that verify whether a particular piece
 * of code is working properly. Unit test methods take no arguments,
 * commit no data to the database, and are flagged with the testMethod
 * keyword in the method definition.
 * All test methods in an organization are executed whenever Apex code is deployed
 * to a production organization to confirm correctness, ensure code
 * coverage, and prevent regressions. All Apex classes are
 * required to have at least 75% code coverage in order to be deployed
 * to a production organization. In addition, all triggers must have some code coverage.
 * The @isTest class annotation indicates this class only contains test
 * methods. Classes defined with the @isTest annotation do not count against
 * the organization size limit for all Apex scripts.
 * See the Apex Language Reference for more information about Testing and Code Coverage.
private class TestProdCase {

    static testMethod void myUnitTest() {
	creating a list to iterate through both states of an Opportunity Case (Submitter, Vendor)
	List<Integer> run = new List<Integer>();
	for(Integer it=0;it <run.size(); it++){
	System.debug('Alerting on ----> '+run);
// 	List<RecordType> rec = [Select id from RecordType where name in ('Implementation Vendor','Implementation Submitter')];
// 	for(Integer it = 0;it<rec.size();it++){


    Create Account Record
    	Account a = new Account(Name='TestAccount');
    	insert a;
    Create Contact Record
	  String acId = null;
      List<Account> ac = [Select id from Account where name = 'TestAccount'];
	  Contact c = new Contact(FirstName='Johnny', LastName='Tester', AccountId=ac[0].Id);
	  acId = ac[0].id;
	  insert c;
	  System.debug('Alerting on ----> '+c);

    Create Opportunity Record
    System.debug('Alerting on ----> '+'Going into the Opportuntiy creation');
    Opportunity o = new Opportunity();
    list<Contact> co = [Select Id from Contact where firstName='Johnny' and lastName = 'Tester'];
   		Integer ct = co.size();
   		System.debug('Alerting on ----> '+ct);
		o.Status__c='Business Development';
		system.debug('Alerting on ----> '+co);
		//Sets the Opportunity type to vendor or submitter
		if(it == 1){
		}else if (it == 2){


		insert o;	
		System.debug('Opportunity Object '+ o);	
	Set the Product
	List<PricebookEntry> numb = [Select Id, Pricebook2Id from PricebookEntry where isActive = TRUE LIMIT 5];
	//OpportunityLineItem opl = new OpportunityLineItem(OpportunityId=o.Id);
	for(Integer i = 0;i<numb.size(); i++){
		OpportunityLineItem opl = new OpportunityLineItem(OpportunityId=o.Id);
//		opl.PricebookEntryId=numb[i].Id;
		insert opl;
	String opp = o.Id;
	Pass in the Opportunity Object
	ProdCase testing = new ProdCase(o);

	Pass  in the Opportunity ID



Hi, Can you confirm the API version. If it is not 21, try updating the API version of your test class to 21.


The api is 21 although I am utilizing the Eclipse plug in for my Salesforce instance.

Shashikant SharmaShashikant Sharma

This error indicates that any query in your test class or most probably in the Apex Code  that you are testing is fetting the data from you Org Data. It an be more than one query that in aggegate causing this issue. Please add more coditions for avoid such query. If You can not provide that check then use then use 


Test.isrunningTest static property to execute different code from test method.