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
FinneyFinney 

System.LimitException: Too many SOQL queries: 101

Hi There,

 

I am getting error when trying to deploy code from sandbox to production.

 

The code is working fine in sandbox and I am lost for thoughts on how to fix this.

 

I am posting the classes, the code that are causing the error and the error log.

 

Please help me.

 

trigger CloseCase on pb__Agreement__c (before update) {
//List<ID> accIds = New List<ID>();
List<ID> agIds =New List<ID> ();
for(pb__Agreement__c ag:Trigger.new){
if(ag.pb__AgreementType__c == 'Sale' && ag.Stage__c == 'Transaction Closed and Commission Received'){
agIds.add(ag.Id);
}
}
List<Case> c = [ SELECT id,Agreement__c,Status, RecordTypeId from Case where Agreement__c = :agIds and RecordTypeId = '012A0000000h1l4' LIMIT 2];
if(c.size() > 0){
Case cs = c[0];
c[0].Status = 'Closed';

}
update c;
}

 

nickwick76nickwick76

Hi,

Can you post the test method that you use and the error messages as well?

 

// Niklas

FinneyFinney

My bad. Sorry I forgot to do that.

 

The error is on the closecase trigger I posted before on Line 9 Column 1

 

UnitTestsCreatePaymentTrigger.myUnitTest System.LimitException: Too many SOQL queries: 101
UnitTestsTenantVacatingProperty.myUnitTest System.LimitException: Too many SOQL queries: 101

23.0

 

The 2 test classes referred in the error -

 

UnitTestsCreatePaymentTrigger

 

@isTest
private class UnitTestsCreatePaymentTrigger {

    static testMethod void myUnitTest() {
        // TO DO: implement unit test
        //create test inventory for SALE
        pb__InventoryItem__c inv = new pb__InventoryItem__c();
        inv.name = 'abc123';
        inv.pb__IsForSale__c = true;
        inv.pb__PurchaseListPrice__c = 12313;
        inv.pb__IsAvailable__c = false;
        inv.Project__c = 'abcdd';
        inv.pb__ItemStatus__c = 'Available';
        inv.pb__IsBlocked__c = false;
        inv.ItemStage__c = 'Available';
        insert inv;
        
        inv.pb__IsAvailable__c=true;
        inv.ItemStage__c = 'Available';
        inv.pb__IsBlocked__c = false;
        update inv;
        
        //create test Compliance user, she will change the agreement status
        //Profile p = [select id from profile where name='Compliance officer'];
        User u = [select id from user where LastName = 'Tan'];
        
        //create test account
        Account a = new Account();
        a.LastName = 'testabcd';
        a.PersonEmail='test@test.com'; 
        a.Phone='85765';
        a.OwnerId = u.id;
        a.service_breach__c = false;
        a.pb__Status__c = 'Unqualified';
        insert a;
        
        a.service_Breach__c = true;
        update a;
       
        
        //create offer on test inventory and account
        pb__Offer__c off = new pb__Offer__c();
        off.pb__InventoryId__c = inv.id;
        off.pb__AccountId__c = a.id;
        off.pb__IsApprovalGranted__c = true;
        off.pb__IsReadyForAgreement__c=true;
        off.pb__OfferType__c = 'Sale';
        insert off;
        
        //generate agreement
        pb__Agreement__c ag = new pb__Agreement__c();
        ag.pb__OfferId__c = off.Id;
        ag.pb__AgreementType__c = 'Sale';
        ag.pb__Status__c = 'Agreement Pending Signatures';
        ag.pb__InventoryId__c = inv.Id;
        ag.payment_created__c = false;
        ag.Transfer_Date__c = date.parse('1/1/2011');
        ag.pb__AgreementDate__c = date.parse('1/1/2011');
        insert ag;
        
        
       
        System.runAs(u){
        ag.pb__status__c = 'Agreement Signed and passed to Conveyance';
        update ag;
        }
        
        
        
        //create test inventory for LEASE
        inv.pb__IsForSale__c = false;
        inv.pb__IsForLease__c=true;
        update inv;
         
        
        //create offer on test inventory and account
        pb__Offer__c off1 = new pb__Offer__c();
        off1.pb__InventoryId__c = inv.id;
        off1.pb__AccountId__c = a.id;
        off1.pb__IsApprovalGranted__c = true;
        off1.pb__IsReadyForAgreement__c=true;
        off1.pb__OfferType__c = 'LEASE';
        insert off1;
        
        //generate agreement
        pb__Agreement__c ag1 = new pb__Agreement__c();
        ag1.pb__OfferId__c = off.Id;
        ag1.pb__AgreementType__c = 'LEASE';
        ag1.pb__Status__c = 'Agreement Pending Signatures';
        ag1.payment_created__c = false;
        ag1.pb__InventoryId__c = inv.id;
        ag1.Transfer_Date__c = date.parse('1/1/2011');
        ag1.pb__AgreementDate__c = date.parse('1/1/2011');
        user u1 = [select id from user where lastname = 'ellison'];
        user u2 = [select id from user where lastname = 'sra'];
        ag1.Agent_1__c = u1.id;
        ag1.Share_agent1__c = 234;
        
        ag1.Agent_2__c = u.id;
        ag1.Share_agent2__c = 234;
        
        ag1.Agent_3__c = u2.id;
        ag1.Share_agent3__c = 234;
        
        insert ag1;
        

        //System.runAs(u){
        //ag1.pb__status__c = 'Agreement Signed and passed to Conveyance';
       // update ag1;//
       // }
    }
}

UnitTestsTenantVacatingProperty

 

 

@isTest
private class UnitTestsTenantVacatingProperty {

    static testMethod void myUnitTest() {
        // TO DO: implement unit test
        //create test inventory for SALE
        pb__InventoryItem__c inv = new pb__InventoryItem__c();
        inv.name = 'abc123';
        inv.pb__IsForSale__c = true;
        inv.pb__PurchaseListPrice__c = 12313;
        inv.pb__IsAvailable__c = false;
        inv.Project__c = 'abcdd';
        inv.pb__ItemStatus__c = 'Available';
        inv.pb__IsBlocked__c = false;
        inv.ItemStage__c = 'Available';
        insert inv;
        
        inv.pb__IsAvailable__c=true;
        inv.ItemStage__c = 'Available';
        inv.pb__IsBlocked__c = false;
        update inv;
        
        //create test Compliance user, she will change the agreement status
        //Profile p = [select id from profile where name='Compliance officer'];
        User u = [select id from user where LastName = 'Tan'];
        
        //create test account
        Account a = new Account();
        a.LastName = 'testabcd-test';
        a.PersonEmail='test@smktest.com'; 
        a.Phone='7890123';
        a.OwnerId = u.id;
        a.service_breach__c = false;
        a.pb__Status__c = 'Unqualified';
        insert a;
        
        a.service_Breach__c = true;
        update a;
       
        
        //create offer on test inventory and account
        pb__Offer__c off = new pb__Offer__c();
        off.pb__InventoryId__c = inv.id;
        off.pb__AccountId__c = a.id;
        off.pb__IsApprovalGranted__c = true;
        off.pb__IsReadyForAgreement__c=true;
        off.pb__OfferType__c = 'Sale';
        insert off;
        
        //generate agreement
        pb__Agreement__c ag = new pb__Agreement__c();
        ag.pb__OfferId__c = off.Id;
        ag.pb__AgreementType__c = 'Sale';
        ag.pb__Status__c = 'Agreement Pending Signatures';
        ag.pb__InventoryId__c = inv.Id;
        ag.payment_created__c = false;
        ag.Transfer_Date__c = date.parse('1/1/2011');
        ag.pb__AgreementDate__c = date.parse('1/1/2011');
        insert ag;
        
        
       
        System.runAs(u){
        ag.pb__status__c = 'Agreement Signed and passed to Conveyance';
        update ag;
        }
        
        
        
        //create test inventory for LEASE
        inv.pb__IsForSale__c = false;
        inv.pb__IsForLease__c=true;
        update inv;
         
        
        //create offer on test inventory and account
        pb__Offer__c off1 = new pb__Offer__c();
        off1.pb__InventoryId__c = inv.id;
        off1.pb__AccountId__c = a.id;
        off1.pb__IsApprovalGranted__c = true;
        off1.pb__IsReadyForAgreement__c=true;
        off1.pb__OfferType__c = 'LEASE';
        insert off1;
        
        //generate agreement
        pb__Agreement__c ag1 = new pb__Agreement__c();
        ag1.pb__OfferId__c = off.Id;
        ag1.pb__AgreementType__c = 'LEASE';
        ag1.pb__Status__c = 'Agreement Pending Signatures';
        ag1.payment_created__c = false;
        ag1.pb__InventoryId__c = inv.id;
        ag1.Transfer_Date__c = date.parse('1/1/2011');
        ag1.pb__AgreementDate__c = date.parse('1/1/2011');
        user u1 = [select id from user where lastname = 'ellison'];
        user u2 = [select id from user where lastname = 'sra'];
        ag1.Agent_1__c = u1.id;
        ag1.Share_agent1__c = 234;
        
        ag1.Agent_2__c = u.id;
        ag1.Share_agent2__c = 234;
        
        ag1.Agent_3__c = u2.id;
        ag1.Share_agent3__c = 234;
        
        insert ag1;
        

        //System.runAs(u){
        //ag1.pb__status__c = 'Agreement Signed and passed to Conveyance';
       // update ag1;//
       // }
    }
}

 

Bhawani SharmaBhawani Sharma
Make sure to use Test.startTest and Test.stopTest. Also check if triggers are not firing recursively.