+ Start a Discussion
Rst123Rst123 

Avoid Too many SOQL queries: 101 in test class?

My code :

@IsTest
private class TestCreditApprovalUtility {
   
    static testmethod void testCalculateAppRemAmount()
      {
       
        //Create test account
        Account account = new Account();
        account.recordtype=new recordtype(name='Customer Record Type');
        account.Name = 'Test';
        account.BillingStreet = '123 Some St.';
        account.BillingCity = 'Somewhere';
        account.BillingState = 'CT';
        account.BillingPostalCode = '12345';
        insert account;
       
        // Create a new Partner Account
        Account partner = new Account();
        partner.recordtype=new recordtype(name='Partner Record Type');
        partner.Name = 'Test Partner';
        partner.BillingStreet = '123 Some St.';
        partner.BillingCity = 'Somewhere';
        partner.BillingState = 'CT';
        partner.BillingPostalCode = '12345';
        insert partner;
       
        // Create a test opportunity to associate with Credit Approvals and Quote
        List<Opportunity> OpList= new List<Opportunity>();
        Opportunity opportunity = new Opportunity();
        opportunity.OwnerId = userinfo.getUserId();
        opportunity.AccountId = account.Id;
        opportunity.Name = 'Test';
        opportunity.StageName = 'Lead';
        opportunity.CloseDate = Date.today();
        opportunity.Amount=1000;
        
        OpList.add(opportunity);    
        insert OpList;
        Set<Id> OpIds = new Set<Id>();
        for(Opportunity o : OpList){
            OpIds.add(o.Id);
        }
        //Changes made for Audit Cleanup :Increase test class coverage
        //Creating test Quote
        Quote__c quote= new Quote__c();
        quote.Product__c='FMV';
        quote.Opportunity__c=opportunity.id;
        quote.Include_For_Risk__c=true;
       
        insert quote;
        //End of Changes made for Audit Cleanup :Increase test class coverage
       
        //Create test takedowns to associate with opportunity
        List<Takedown__c> takedowns= new List<Takedown__c>();
        Takedown__c Tk1= new Takedown__c();
        Tk1.Name= 'Test T1';
        Tk1.Opportunity__c= opportunity.id;
        Tk1.CurrencyIsoCode= 'USD';
        Tk1.Total_Amount_Financed__c=1000.00;
       
        takedowns.add(Tk1);
       
        Takedown__c Tk2= new Takedown__c();
        Tk2.Name= 'Test T2';
        Tk2.Opportunity__c= opportunity.id;
        Tk2.CurrencyIsoCode= 'USD';
        Tk2.Total_Amount_Financed__c=700.00;
       
        takedowns.add(Tk2);
       
        insert takedowns;
       
        Utility.isOwnerUpdate=true;
        Utility.IsStageChange=true;
                 
       
        //Create test Credit Approvals
        List<Credit_Approval__c> CAList= new List<Credit_Approval__c>();
        Credit_Approval__c CA1=new Credit_Approval__c();
        CA1.Partner__c=partner.id;
        CA1.Account__c=account.id ;
        CA1.recordtype=new recordtype(name='EF HFS Credit Approval Record Type');
        CA1.Approved_Amount_With_Tolerance__c= 10000;
        CA1.Approved_Amount__c= 10000;
        CA1.Application_Type__c= 'Line Of Credit';
        CA1.Submittal_Status__c= 'Decisioned';
        CA1.Risk_Decision__c='Approved';
        CA1.LOC_Adjustment_Amount__c= 200.00;
        //CA1.Approval_Expiration_Date__c= myDate;
        CAList.add(CA1);
       
       
        Credit_Approval__c CA2=new Credit_Approval__c();
        CA2.Partner__c=partner.id;
        CA2.Account__c=account.id ;
        CA2.recordtype=new recordtype(name='EF HFS Credit Approval Record Type');
        CA2.Approved_Amount_With_Tolerance__c= 100.00;
        CA2.Approved_Amount__c= 10000;
        //CA2.LOC_Adjustment_Amount__c= 200.00;
        CA2.Application_Type__c= 'Line Of Credit';
        CA2.Submittal_Status__c= 'Referred';
        CA2.Risk_Decision__c='Approved';
        //CA2.Credit_Line_ID__c= creditLine2.id;
        //CA2.Approval_Expiration_Date__c= myDate;
        CAList.add(CA2);
       
        Credit_Approval__c CA3=new Credit_Approval__c();
        CA3.Partner__c=partner.id;
        CA3.Account__c=account.id ;
        CA3.recordtype=new recordtype(name='EF HFS Credit Approval Record Type');
        CA3.Approved_Amount_With_Tolerance__c= 100.00;
        CA3.Approved_Amount__c= 10000;
        CA3.LOC_Adjustment_Amount__c= 200.00;
        CA3.Application_Type__c= 'Deal - Multiple Takedown';
        CA3.Submittal_Status__c= 'Test';
        CA3.Risk_Decision__c='Approved';
        //CA3.Credit_Line_ID__c= creditLine2.id;
        //CA3.Approval_Expiration_Date__c= myDate;
        CA3.Opportunity__c=opportunity.id;
        CAList.add(CA3);
       
        Credit_Approval__c CA4=new Credit_Approval__c();
        CA4.Partner__c=partner.id;
        CA4.Account__c=account.id ;
        CA4.recordtype=new recordtype(name='EF HFS Credit Approval Record Type');
        CA4.Approved_Amount_With_Tolerance__c= 10000.00;
        CA4.Approved_Amount__c= 10000;
        CA4.LOC_Adjustment_Amount__c= 200.00;
        CA4.Application_Type__c= 'Deal - Multiple Takedown';
        CA4.Submittal_Status__c= 'Decisioned';
        CA4.Risk_Decision__c='Approved';
        //CA4.Credit_Line_ID__c= creditLine2.id;
        //CA4.Approval_Expiration_Date__c= myDate;
        CA4.Opportunity__c=opportunity.id;
        CAList.add(CA4);      
        
        insert CAList;
        Set<Id> CaIds = new Set<Id>();
        for(Credit_Approval__c c : CAList){
            CaIds.add(c.Id);
        }
           
        //Create test Credit Approvals to make them as child Credit Approvals
        List<Credit_Approval__c> CreditLineList= new List<Credit_Approval__c>();
        Credit_Approval__c creditLine1 =new Credit_Approval__c();
        creditLine1.Account__c=account.id ;
        creditLine1.recordtype=new recordtype(name='EF HFS Credit Approval Record Type');
        creditLine1.Application_Type__c= 'Deal - Multiple Takedown';
        creditLine1.Approved_Amount_With_Tolerance__c= 1000.00;
        creditLine1.Approved_Amount__c= 200.00;
        creditLine1.Submittal_Status__c= 'Decisioned';
        creditLine1.Risk_Decision__c='Approved';
        creditLine1.Credit_Line_ID__c= CA1.id;
       
        CreditLineList.add(creditLine1);
       
        Credit_Approval__c creditLine2 =new Credit_Approval__c();
        creditLine2.Account__c=account.id ;
        creditLine2.recordtype=new recordtype(name='EF HFS Credit Approval Record Type');
        creditLine2.Application_Type__c= 'Deal - Multiple Takedown';
        creditLine2.Approved_Amount_With_Tolerance__c= 100.00;
        creditLine2.Approved_Amount__c= 200.00;
        creditLine2.Submittal_Status__c= 'Referred';
        creditLine2.Risk_Decision__c='Approved';
        creditLine2.Credit_Line_ID__c= CA2.id;
        creditLine2.LOC_Adjustment_Amount__c= 200.00;
       
        CreditLineList.add(creditLine2);
       
        Credit_Approval__c creditLine3 =new Credit_Approval__c();
        creditLine3.Account__c=account.id ;
        creditLine3.recordtype=new recordtype(name='EF HFS Credit Approval Record Type');
        creditLine3.Application_Type__c= 'Deal - Multiple Takedown';
        creditLine3.Approved_Amount_With_Tolerance__c= 100.00;
        creditLine3.Approved_Amount__c= 200.00;
        creditLine3.Submittal_Status__c= 'Referred';
        creditLine3.Risk_Decision__c='Approved';
        creditLine3.Credit_Line_ID__c= null;
        creditLine3.LOC_Adjustment_Amount__c= 200.00;
       
        CreditLineList.add(creditLine3);     
               
        insert CreditLineList;
        
        Test.startTest();
        //CA1.Approved_Amount_With_Tolerance__c= 20000;
        //update CAList;
       
        Map<Id,Decimal> UsedAmt= CreditApprovalUtility.GetUsedAmounts(CaIds);
        List<Credit_Approval__c> ca = [Select Id,Amount_Approved_Remaining__c from Credit_Approval__c where Id in :CaIds];
        Map<Id,decimal> RemAm= CreditApprovalUtility.CalculateAppRemAmount(null);
        Map<Id,decimal> RemAmount= CreditApprovalUtility.CalculateAppRemAmount(CAList);
        system.assertEquals(9800.00,RemAmount.get(CA1.id));
        Map<Id,decimal> RemAmountt= CreditApprovalUtility.CalculateAppRemAmount(CreditLineList);
       
              Map<Id,double> PaymentStreamTerm= CreditApprovalUtility.GetPaymentStreamTerm(OpIds);
        system.assertEquals(0.0,PaymentStreamTerm.get(opportunity.id));
               
        Test.stopTest();
       
      }
   
}

Can you help in revoving this error
Vinita_SFDCVinita_SFDC
Hello,

Code seems absolutely fine, can you check in developer console what this query is returning:

Select Id,Amount_Approved_Remaining__c from Credit_Approval__c where Id in :CaIds