function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
sfdc007sfdc007 

Too many soql queries 101 test class

Hi,

I have a apex class which is having a test class on running which is throwing too many soql queries 101

need help on it pls
 
MY APEX CLASS :

Public class dqtCustomSimple

{
public String dlmstage { get;set;}    

  public dqtCustomSimple()
  
   {
 
   dlmstage = ApexPages.currentPage().getParameters().get('dlmstageid');
      
   }
   Public pagereference redirecttostage()
  
 {
 PageReference newpage= new PageReference('/'+ dlmstage);
      newpage.setRedirect(true);
return newpage;
}

  }
MY TEST CLASS :

@isTest(seeallData = true)
private class dqtCustomSimple_Test
{
    
  static Stage__c stg;
  static Opportunity opp;
   static User U;
   static Engagement_Script__c EP;
    

   
     static testMethod void dqtCustomSimple_Test()
    {
         Test.StartTest();
        CreateData();             
        CreateDQTStage();
       
      
        system.currentpagereference().getparameters().put('id',stg.Id);  
        dqtCustomSimple objdqtCustom = new dqtCustomSimple();
        objdqtCustom.redirecttostage();
       Test.StopTest();
     }
     
     public static void  CreateData()
     {
	 Test.StartTest();
     U = Utils_TestMethods.createStandardUser('alias');  
     insert U;
     System.runAs(U){
     List <PriceBook2> pb2list = new List<PriceBook2>();
     List <Product2> prodlist = new List<Product2>();
     List <PriceBookEntry> pbelist = new List<PriceBookEntry>();
     List <PriceBookEntry> stdPbelist = new List<PriceBookEntry>();
     integer quantity;
      Account acc = Utils_TestMethods.createAccount();
      insert acc;
       acc.ActualUnmanaged__c = true; 
       Database.update(acc);
       system.debug('acc==>'+acc);
     pricebook2 pb2 = [select Id from Pricebook2 where name='Wireline Price Book' and IsActive=true limit 1];
     SFDCtoECSfromTrigger.isttesting = true;  
     opp = Utils_TestMethods.createOpportunity(acc,u);
     insert opp; 
       opp.DLM_Region__c   = 'APAC';
       Database.update(opp); 
       system.debug('opp==>'+opp);
       
      product2 p2= new product2(name='p2test',product_id__c='p2test',isActive=true);
      insert p2;    
      
      pricebookentry pbe = [select Pricebook2Id from pricebookentry where pricebook2id=:pb2.id and product2id =: p2.id limit 1];
    
      Exchange_Rate__c exr=new Exchange_Rate__c(Name='USD',Conversion_Rate__c=10); 
      insert exr;
      Test.StopTest();
      OpportunityLineItem olm=new OpportunityLineItem(Pricebookentryid=pbe.id,Local_Currency__c=exr.id,Local_Booking__c=123455,Opportunityid=opp.id );
      insert olm;
       system.debug('olm==>'+olm);
       
      
      }
    }
    public static void CreateDQTStage()
    {
		 Test.StartTest();

	
        EP = new Engagement_Script__c();
      EP.Engagement_Status__c = 'Open';
      EP.Opportunity__c = opp.Id ;        
      insert EP; 
      system.debug('EP==>'+EP);  
        
     stg = Utils_TestMethods.createStage(opp);
    string strStageName;
    Id rectypeid =[select Id from RecordType where Name=:'Stage 1 RRB'  and SobjectType='Stage__c' limit 1].Id;
    stg.RecordTypeId =rectypeid;
    stg.PCM__c= U.ID;
    stg.Sales__c= U.ID;
    stg.SDA__c = U.ID;                   
    stg.Engagement_Script__c = EP.Id;
    stg.DQT_Approval_Status__c='Approved';    
    insert stg;
       system.debug('stg==>'+stg); 
       
     Stage_Team__c stgTeam = Utils_TestMethods.createStageTeam(stg,U);
     Insert stgTeam; 
	 Test.StopTest();
     stgTeam.User_Role__c = 'Business Operations';
     Database.update(stgTeam);  

        
    }
    
  
    
    
    
}
 It is giving error in thi line

dqtCustomSimple_Test

Kindly help me pls

Thanks in advnace

 
Best Answer chosen by sfdc007
Amit Chaudhary 8Amit Chaudhary 8
Try below code:-
@isTest(seeallData = true)
private class dqtCustomSimple_Test
{
	static Stage__c stg;
	static Opportunity opp;
	static User U;
	static Engagement_Script__c EP;
	
    static testMethod void dqtCustomSimple_Test()
    {
        CreateData();             

		Test.StartTest();
		CreateDQTStage();
			system.currentpagereference().getparameters().put('id',stg.Id);  
			dqtCustomSimple objdqtCustom = new dqtCustomSimple();
			objdqtCustom.redirecttostage();
       Test.StopTest();
     }
     
     public static void  CreateData()
     {
	 Test.StartTest();
     U = Utils_TestMethods.createStandardUser('alias');  
     insert U;
     System.runAs(U){
     List <PriceBook2> pb2list = new List<PriceBook2>();
     List <Product2> prodlist = new List<Product2>();
     List <PriceBookEntry> pbelist = new List<PriceBookEntry>();
     List <PriceBookEntry> stdPbelist = new List<PriceBookEntry>();
     integer quantity;
      Account acc = Utils_TestMethods.createAccount();
      insert acc;
       acc.ActualUnmanaged__c = true; 
       Database.update(acc);
       system.debug('acc==>'+acc);
     pricebook2 pb2 = [select Id from Pricebook2 where name='Wireline Price Book' and IsActive=true limit 1];
     SFDCtoECSfromTrigger.isttesting = true;  
     opp = Utils_TestMethods.createOpportunity(acc,u);
     insert opp; 
       opp.DLM_Region__c   = 'APAC';
       Database.update(opp); 
       system.debug('opp==>'+opp);
       
      product2 p2= new product2(name='p2test',product_id__c='p2test',isActive=true);
      insert p2;    
      
      pricebookentry pbe = [select Pricebook2Id from pricebookentry where pricebook2id=:pb2.id and product2id =: p2.id limit 1];
    
      Exchange_Rate__c exr=new Exchange_Rate__c(Name='USD',Conversion_Rate__c=10); 
      insert exr;
      Test.StopTest();
      OpportunityLineItem olm=new OpportunityLineItem(Pricebookentryid=pbe.id,Local_Currency__c=exr.id,Local_Booking__c=123455,Opportunityid=opp.id );
      insert olm;
       system.debug('olm==>'+olm);
       
      
      }
    }
    public static void CreateDQTStage()
    {
      EP = new Engagement_Script__c();
      EP.Engagement_Status__c = 'Open';
      EP.Opportunity__c = opp.Id ;        
      insert EP; 
      system.debug('EP==>'+EP);  
        
		 stg = Utils_TestMethods.createStage(opp);
		string strStageName;
		Id rectypeid =[select Id from RecordType where Name=:'Stage 1 RRB'  and SobjectType='Stage__c' limit 1].Id;
		stg.RecordTypeId =rectypeid;
		stg.PCM__c= U.ID;
		stg.Sales__c= U.ID;
		stg.SDA__c = U.ID;                   
		stg.Engagement_Script__c = EP.Id;
		stg.DQT_Approval_Status__c='Approved';    
		insert stg;
       system.debug('stg==>'+stg); 
       
     Stage_Team__c stgTeam = Utils_TestMethods.createStageTeam(stg,U);
     Insert stgTeam; 
	 
     stgTeam.User_Role__c = 'Business Operations';
     Database.update(stgTeam);  

        
    }
    
  
    
    
    
}

Please let us know if this will help you

 

All Answers

Amit Chaudhary 8Amit Chaudhary 8
Please try below code:-
@isTest(seeallData = true)
private class dqtCustomSimple_Test
{
	static Stage__c stg;
	static Opportunity opp;
	static User U;
	static Engagement_Script__c EP;
	
    static testMethod void dqtCustomSimple_Test()
    {
        CreateData();             

		Test.StartTest();
		CreateDQTStage();
			system.currentpagereference().getparameters().put('id',stg.Id);  
			dqtCustomSimple objdqtCustom = new dqtCustomSimple();
			objdqtCustom.redirecttostage();
       Test.StopTest();
     }
     
     public static void  CreateData()
     {
	 Test.StartTest();
     U = Utils_TestMethods.createStandardUser('alias');  
     insert U;
     System.runAs(U){
     List <PriceBook2> pb2list = new List<PriceBook2>();
     List <Product2> prodlist = new List<Product2>();
     List <PriceBookEntry> pbelist = new List<PriceBookEntry>();
     List <PriceBookEntry> stdPbelist = new List<PriceBookEntry>();
     integer quantity;
      Account acc = Utils_TestMethods.createAccount();
      insert acc;
       acc.ActualUnmanaged__c = true; 
       Database.update(acc);
       system.debug('acc==>'+acc);
     pricebook2 pb2 = [select Id from Pricebook2 where name='Wireline Price Book' and IsActive=true limit 1];
     SFDCtoECSfromTrigger.isttesting = true;  
     opp = Utils_TestMethods.createOpportunity(acc,u);
     insert opp; 
       opp.DLM_Region__c   = 'APAC';
       Database.update(opp); 
       system.debug('opp==>'+opp);
       
      product2 p2= new product2(name='p2test',product_id__c='p2test',isActive=true);
      insert p2;    
      
      pricebookentry pbe = [select Pricebook2Id from pricebookentry where pricebook2id=:pb2.id and product2id =: p2.id limit 1];
    
      Exchange_Rate__c exr=new Exchange_Rate__c(Name='USD',Conversion_Rate__c=10); 
      insert exr;
      Test.StopTest();
      OpportunityLineItem olm=new OpportunityLineItem(Pricebookentryid=pbe.id,Local_Currency__c=exr.id,Local_Booking__c=123455,Opportunityid=opp.id );
      insert olm;
       system.debug('olm==>'+olm);
       
      
      }
    }
    public static void CreateDQTStage()
    {
		 Test.StartTest();

	
        EP = new Engagement_Script__c();
      EP.Engagement_Status__c = 'Open';
      EP.Opportunity__c = opp.Id ;        
      insert EP; 
      system.debug('EP==>'+EP);  
        
     stg = Utils_TestMethods.createStage(opp);
    string strStageName;
    Id rectypeid =[select Id from RecordType where Name=:'Stage 1 RRB'  and SobjectType='Stage__c' limit 1].Id;
    stg.RecordTypeId =rectypeid;
    stg.PCM__c= U.ID;
    stg.Sales__c= U.ID;
    stg.SDA__c = U.ID;                   
    stg.Engagement_Script__c = EP.Id;
    stg.DQT_Approval_Status__c='Approved';    
    insert stg;
       system.debug('stg==>'+stg); 
       
     Stage_Team__c stgTeam = Utils_TestMethods.createStageTeam(stg,U);
     Insert stgTeam; 
	 Test.StopTest();
     stgTeam.User_Role__c = 'Business Operations';
     Database.update(stgTeam);  

        
    }
    
  
    
    
    
}

NOTE:- try to avoid seeAllData= true in test classes
Please check best pratice for test classes in below blog:-
http://amitsalesforce.blogspot.in/2015/06/best-practice-for-test-classes-sample.html

Please let us know if this will help you.

Thanks,
Amit Chaudhary
sfdc007sfdc007
i tried the above code

getting the folowing error

testing already started

Error Message System.FinalException: Testing already started
Stack Trace (System Code)
Class.dqtCustomSimple_Test.CreateDQTStage: line 63, column 1
Class.dqtCustomSimple_Test.dqtCustomSimple_Test: line 15, column 1
Amit Chaudhary 8Amit Chaudhary 8
Try below code:-
@isTest(seeallData = true)
private class dqtCustomSimple_Test
{
	static Stage__c stg;
	static Opportunity opp;
	static User U;
	static Engagement_Script__c EP;
	
    static testMethod void dqtCustomSimple_Test()
    {
        CreateData();             

		Test.StartTest();
		CreateDQTStage();
			system.currentpagereference().getparameters().put('id',stg.Id);  
			dqtCustomSimple objdqtCustom = new dqtCustomSimple();
			objdqtCustom.redirecttostage();
       Test.StopTest();
     }
     
     public static void  CreateData()
     {
	 Test.StartTest();
     U = Utils_TestMethods.createStandardUser('alias');  
     insert U;
     System.runAs(U){
     List <PriceBook2> pb2list = new List<PriceBook2>();
     List <Product2> prodlist = new List<Product2>();
     List <PriceBookEntry> pbelist = new List<PriceBookEntry>();
     List <PriceBookEntry> stdPbelist = new List<PriceBookEntry>();
     integer quantity;
      Account acc = Utils_TestMethods.createAccount();
      insert acc;
       acc.ActualUnmanaged__c = true; 
       Database.update(acc);
       system.debug('acc==>'+acc);
     pricebook2 pb2 = [select Id from Pricebook2 where name='Wireline Price Book' and IsActive=true limit 1];
     SFDCtoECSfromTrigger.isttesting = true;  
     opp = Utils_TestMethods.createOpportunity(acc,u);
     insert opp; 
       opp.DLM_Region__c   = 'APAC';
       Database.update(opp); 
       system.debug('opp==>'+opp);
       
      product2 p2= new product2(name='p2test',product_id__c='p2test',isActive=true);
      insert p2;    
      
      pricebookentry pbe = [select Pricebook2Id from pricebookentry where pricebook2id=:pb2.id and product2id =: p2.id limit 1];
    
      Exchange_Rate__c exr=new Exchange_Rate__c(Name='USD',Conversion_Rate__c=10); 
      insert exr;
      Test.StopTest();
      OpportunityLineItem olm=new OpportunityLineItem(Pricebookentryid=pbe.id,Local_Currency__c=exr.id,Local_Booking__c=123455,Opportunityid=opp.id );
      insert olm;
       system.debug('olm==>'+olm);
       
      
      }
    }
    public static void CreateDQTStage()
    {
      EP = new Engagement_Script__c();
      EP.Engagement_Status__c = 'Open';
      EP.Opportunity__c = opp.Id ;        
      insert EP; 
      system.debug('EP==>'+EP);  
        
		 stg = Utils_TestMethods.createStage(opp);
		string strStageName;
		Id rectypeid =[select Id from RecordType where Name=:'Stage 1 RRB'  and SobjectType='Stage__c' limit 1].Id;
		stg.RecordTypeId =rectypeid;
		stg.PCM__c= U.ID;
		stg.Sales__c= U.ID;
		stg.SDA__c = U.ID;                   
		stg.Engagement_Script__c = EP.Id;
		stg.DQT_Approval_Status__c='Approved';    
		insert stg;
       system.debug('stg==>'+stg); 
       
     Stage_Team__c stgTeam = Utils_TestMethods.createStageTeam(stg,U);
     Insert stgTeam; 
	 
     stgTeam.User_Role__c = 'Business Operations';
     Database.update(stgTeam);  

        
    }
    
  
    
    
    
}

Please let us know if this will help you

 
This was selected as the best answer
sfdc007sfdc007
thanks dude , it worked