+ Start a Discussion
SF7SF7 

System.LimitException: Too many SOQL queries: 101 Only in Production not in UAT

Hi,

I have a code which is causing issues while deploying . Pb is it is fine in UAT and not in Production,
TestOppLineItemControllerExtension testChangeLOB System.LimitException: Too many SOQL queries: 101 
Stack Trace: Class.OppLineItemControllerExtension.<init>: line 131, column 1 Class.TestOppLineItemControllerExtension.testChangeLOB: line 231, column 1
TestOppLineItemControllerExtension testInsertOLI System.LimitException: Too many SOQL queries: 101 
Stack Trace: Class.OppLineItemControllerExtension.<init>: line 131, column 1 Class.TestOppLineItemControllerExtension.testInsertOLI: line 57, column 1
TestOppLineItemControllerExtension testPageMessages System.LimitException: Too many SOQL queries: 101 
Stack Trace: Class.OppLineItemControllerExtension.<init>: line 131, column 1 Class.TestOppLineItemControllerExtension.testPageMessages: line 169, column 1
TestOppLineItemControllerExtension testcancelMethod System.LimitException: Too many SOQL queries: 101 
Stack Trace: Class.OppLineItemControllerExtension.<init>: line 131, column 1 Class.TestOppLineItemControllerExtension.testcancelMethod: line 202, column 1
 
static testMethod void testChangeLOB() 
    {
        //Create test data
        Opportunity opp = new opportunity();
        opp = createTestData();
        // Set the current page to OpportunityLineItemEdit VF Page  
        PageReference servicesPage = new PageReference('apex/OpportunityLineItemEdit?opp_id='+opp.id);
        Test.setCurrentPage(servicesPage);
        //Create an object of the OppLineItemControllerExtension class
        OppLineItemControllerExtension oliCE = new OppLineItemControllerExtension ();
                        
        //The values of service Line and sub-service are not null
        system.assertnotEquals(null, oliCE.selFamily);
        system.assertEquals(null, oliCE.selService);
        
        //re-populate the value of LOB and invoke the changeLOB method
    
        oliCE.changeLOB();
        //Retrieve the values of service line and sub-service
      
        // The service line will be none because the LOB has changed
        system.assertEquals('-None-', oliCE.selFamily);
         // The sub-service  will be null because the service line is NONE
        system.assertEquals(null, oliCE.selService);
                     
    }
    
     
}
 
static testMethod void testcancelMethod() 
    {
         //Create test data
        Opportunity opp = new opportunity();
        opp = createTestData();
        // Set the current page to OpportunityLineItemEdit VF Page  
        PageReference servicesPage = new PageReference('apex/OpportunityLineItemEdit?opp_id='+opp.id);
        Test.setCurrentPage(servicesPage);
        //Create an object of the OppLineItemControllerExtension class
        OppLineItemControllerExtension oliCE = new OppLineItemControllerExtension ();
        //Invoke the cancel method and verify the url of the page it navigates to
        String nextpage = oliCE.cancel().getUrl();
        PageReference p = new ApexPages.Standardcontroller (opp).view ();
        
        system.assertequals(p.getUrl(),nextpage);
       
    }
 
This is the class code part where the stack is pointing 

public OppLineItemControllerExtension() 
  {
       selLOB = '-None-'; 
       selFamily = '-None-';
       
       if ((ApexPages.currentPage().getParameters().get('opp_id') != null)&&(ApexPages.currentPage().getParameters().get('opp_id').length() > 0))
       {
         System.debug ('*****Opp in URL: '+ ApexPages.currentPage().getParameters().get('opp_id').length());
         this.oli = new OpportunityLineItem();
         oli.OpportunityId = ApexPages.currentPage().getParameters().get('opp_id');
       
           opp = [select Name, Account.name, CurrencyIsoCode,IsClosed ,ServiceEdit__c from Opportunity where id=:oli.OpportunityId];
       accountName = opp.Account.Name;    
       oppName = opp.Name;
       oppCurrencyIsoCode = opp.CurrencyIsoCode;
       
       
    for (pricebookentry pbe : [SELECT product2.recordtype.name,  product2.family, product2.Sub_Service__c, product2Id from pricebookentry where pricebook2.name like 'OPP%' and isActive = TRUE and pricebookentry.CurrencyIsoCode =: oppCurrencyIsoCode order by product2.recordtype.name, product2.family, product2.Sub_Service__c])
    {
      if (LOBName != pbe.product2.recordtype.name)
      {
        LOBName = pbe.product2.recordtype.name;
        System.debug('LOB NAME::::'+LOBName);
        LOBServicesMap.put (LOBName, new Map <String, Map <ID, String>> ());
      }
      
      if (Family != pbe.product2.family)
      {
        Family = pbe.product2.family;
        LOBServicesMap.get (LOBName).put (Family, new Map <ID, String> ());
      }
      
      LOBServicesMap.get (LOBName).get (Family).put (pbe.id, pbe.product2.Sub_Service__c);
        
    }
    displayOLIDetails = true;
   }
   //PU:03/04/2011 - When the Opportunity Id is null the below error message has to be displayed.
      
   else
   {
       displayOLIDetails = false;
       ApexPages.addMessage(new ApexPages.Message (ApexPages.Severity.Error, Label.oppLI_ErrMsgOppRequired));
                
   }
   
   wrappers=new List<OcrWrapper>();
   for (Integer idx=0; idx<1; idx++)
   {
           List<selectOption> opt = LOBNames();
           wrappers.add(new OcrWrapper(nextIdent+1,opt));
   }
 }

Thanks 
Akhil
Best Answer chosen by SF7
Balaji BondarBalaji Bondar
Hi Akhil,

Try Test.startTest() and Test.stopTest() to allow to reset the governor limits within the context of your test execution
static testMethod void testChangeLOB() {
        //Create test data
        Opportunity opp = new opportunity();
        opp = createTestData();
        // Set the current page to OpportunityLineItemEdit VF Page  
        PageReference servicesPage = new PageReference('apex/OpportunityLineItemEdit?opp_id='+opp.id);
        Test.setCurrentPage(servicesPage);
        Test.startTest();
		//Create an object of the OppLineItemControllerExtension class
        OppLineItemControllerExtension oliCE = new OppLineItemControllerExtension ();
                        
        //The values of service Line and sub-service are not null
        system.assertnotEquals(null, oliCE.selFamily);
        system.assertEquals(null, oliCE.selService);
        
        //re-populate the value of LOB and invoke the changeLOB method
    
        oliCE.changeLOB();
        //Retrieve the values of service line and sub-service
      
        // The service line will be none because the LOB has changed
        system.assertEquals('-None-', oliCE.selFamily);
         // The sub-service  will be null because the service line is NONE
        system.assertEquals(null, oliCE.selService);
		Test.stopTest()
	}

Important :
If this is what you were looking for then please mark it as a "SOLUTION" or You can Click on the "Like" Button if this was beneficial for you.

All Answers

Balaji BondarBalaji Bondar
Hi Akhil,

Try Test.startTest() and Test.stopTest() to allow to reset the governor limits within the context of your test execution
static testMethod void testChangeLOB() {
        //Create test data
        Opportunity opp = new opportunity();
        opp = createTestData();
        // Set the current page to OpportunityLineItemEdit VF Page  
        PageReference servicesPage = new PageReference('apex/OpportunityLineItemEdit?opp_id='+opp.id);
        Test.setCurrentPage(servicesPage);
        Test.startTest();
		//Create an object of the OppLineItemControllerExtension class
        OppLineItemControllerExtension oliCE = new OppLineItemControllerExtension ();
                        
        //The values of service Line and sub-service are not null
        system.assertnotEquals(null, oliCE.selFamily);
        system.assertEquals(null, oliCE.selService);
        
        //re-populate the value of LOB and invoke the changeLOB method
    
        oliCE.changeLOB();
        //Retrieve the values of service line and sub-service
      
        // The service line will be none because the LOB has changed
        system.assertEquals('-None-', oliCE.selFamily);
         // The sub-service  will be null because the service line is NONE
        system.assertEquals(null, oliCE.selService);
		Test.stopTest()
	}

Important :
If this is what you were looking for then please mark it as a "SOLUTION" or You can Click on the "Like" Button if this was beneficial for you.
This was selected as the best answer
SF7SF7
Hi Balaji, 

Thanks for the reply . It  worked for all the other 4 methods methods . Thank you very much