+ Start a Discussion
daniel1110daniel1110 

Too many SOQL queries 101 in testClass w/o for loop

Hi. I'm not using any for loop but getting System.LimitException: Too many SOQL queries: 101

 

The error is recognized in insert(testTask)

 

Sorry I'm not a good coder. Any suggestions would be appreciated. Thank you in advance.

 

@isTest
public class updateSCBonT4Test {
         static testMethod void testupdateSCBonT4(){
             Account testAccount = new Account(name='test account');
             insert(testAccount);
             Stem_Cell_Bank__c s = new Stem_Cell_Bank__c();
             s.name = 'Test';
//             system.debug('value is = ' + testAccount.Id);
             s.account__c = testAccount.Id;
             test.startTest(); 
             insert(s); 
             LIST<Stem_Cell_Bank__c> a = [SELECT Id, Stage__c FROM Stem_Cell_Bank__c WHERE Name = 'Test'];
             Task testTask = new Task(Subject

 = 'Collect Enrollment Information', whatId = a[0].ID); 
             insert(testTask); 
             Stem_Cell_Bank__c updatedStem_Cell_Bank = [SELECT stage__c FROM Stem_Cell_Bank__c WHERE name = 'Test'];
             system.AssertEquals('Collect Enrollment Information',updatedStem_Cell_Bank.stage__c);
             testTask.whatId = s.Id;
             testTask.Status='Completed';
             update(testTask);

             Task testTask1 = new Task(Subject = 'Process Payment', whatId = a[0].ID); 
             insert(testTask1); 
             Stem_Cell_Bank__c updatedStem_Cell_Bank1 = [SELECT stage__c FROM Stem_Cell_Bank__c WHERE name = 'Test'];
             system.AssertEquals('Process Payment',updatedStem_Cell_Bank1.stage__c); 
             testTask1.whatId = s.Id;
             testTask1.Status='Completed';
             update(testTask1);

             Task testTask2 = new Task(Subject = 'Order Blood Test', whatId = a[0].ID); 
             insert(testTask2); 
             Stem_Cell_Bank__c updatedStem_Cell_Bank2 = [SELECT stage__c FROM Stem_Cell_Bank__c WHERE name = 'Test'];
             system.AssertEquals('Order Blood Test',updatedStem_Cell_Bank2.stage__c); 
             testTask2.whatId = s.Id;
             testTask2.Status='Completed';
             update(testTask2);

             Task testTask3 = new Task(Subject = 'Schedule Fat Extraction', whatId = a[0].ID); 
             insert(testTask3); 
             Stem_Cell_Bank__c updatedStem_Cell_Bank3 = [SELECT stage__c FROM Stem_Cell_Bank__c WHERE name = 'Test'];
             system.AssertEquals('Schedule Fat Extraction',updatedStem_Cell_Bank3.stage__c); 
             testTask3.whatId = s.Id;
             testTask3.Status='Completed';
             update(testTask3);
             
             Stem_Cell_Bank__c checkupdate = [SELECT stage__c, id FROM Stem_Cell_Bank__c WHERE name='Test'];
             System.debug(checkupdate.Id);
             System.debug(checkupdate.Stage__c);             
             system.AssertEquals('Review Eligibility Test',checkupdate.stage__c); 

             test.stopTest();
        }
}

 

Arun MKArun MK

Hi,

 

Since its a test class, this is not just about the numbdr of SOQL queries inside this test class. When this test class is executed, there will be triggers associated to the objects where you are performing the DML opertaions.

 

In your case, there might be triggers running when you insert account, task, Stem_Cell_Bank__c, etc., And these triggers might have SOQL queries in it. May be these triggers might have a SOQL qery running inside a for loop in one of these triggers.

 

Regards,

Arun.

daniel1110daniel1110

Hi Arun_lister, that's very helpful insight. I have about 6 triggers that does some variations of the the following:

 

trigger updateSCBonTask6 on Task (after update) {

    Set<Id> SCBIds = new Set<Id>();

    for(Task t: [SELECT Subject,WhatId, Status FROM Task WHERE ID IN:trigger.new]){

        String wId = t.WhatId;
    
        if(wId!=null && !SCBIds.contains(t.WhatId) && t.Status == 'Completed' && t.Subject.Contains('Harvest Stem Cells')){
            SCBIds.add(t.WhatId);
        }
    }
    
    for(Stem_Cell_Bank__c a:[SELECT stage__c FROM Stem_Cell_Bank__c WHERE ID IN:SCBIds]){
                 a.stage__c = '	Stem Cells Banked';
        update a; 
    }  

}

 

Do you think the problem is here?

 

Thank you

Arun MKArun MK

Hi daniel1110,

 

In this trigger, the DML operation "update a" cannot be inside the for loop. And u need not query the task object again in the first for loop.

 

But I still don't feel the error you reported might be bcoz of this triiger as I don't see any SOQL query inside a for loop. Still you can try the below and see if it is fixed.

 

trigger updateSCBonTask6 on Task (after update) {

    

    Set<Id> SCBIds = new Set<Id>();

    

    for(Task t: trigger.new){

        

        String wId = t.WhatId;

        

        if(wId!=null && !SCBIds.contains(t.WhatId) && t.Status == 'Completed' && t.Subject.Contains('Harvest Stem Cells')){

            SCBIds.add(t.WhatId);

        }

    }

    

    List<Stem_Cell_Bank__c> stemCellBankList = new List<>(Sten_Cell_Bank__c);

    

    for(Stem_Cell_Bank__c a:[SELECT stage__c FROM Stem_Cell_Bank__c WHERE ID IN:SCBIds]){

        a.stage__c = 'Stem Cells Banked';

        stemCellBankList.add(a);

    }

    

    update stemCellBankList;

    

}

 

 

 

daniel1110daniel1110

Hi! I tried your suggestion, but it didn't work... :(

 
Here's another trigger that this custom object uses that's complex (at least to me)
 
trigger createOrderforABFSCB on Stem_Cell_Bank__c (after update) {

    List<ChargentOrders__ChargentOrder__c> co = new List<ChargentOrders__ChargentOrder__c>();  
   
  for (Stem_Cell_Bank__c scb : Trigger.new) {
//    Stem_Cell_Bank__c oscb = Trigger.oldMap.get(scb.ID); /* new line */
    if (scb.Stage__c == 'Stem Cells Banked' && Trigger.oldMap.get(scb.Id).Stage__c != 'Stem Cells Banked')  { /*scb to oscb*/
      
      //initiate the object to put values for furture record
      ChargentOrders__ChargentOrder__c c = new ChargentOrders__ChargentOrder__c();

      //map Order fields to SCB that is being created for this Order
      c.Stem_Cell_Bank__c = scb.Id; /*scb to oscb*/
          c.RecordTypeId = '012E0000000VPOHIA4';

          c.ChargentOrders__Date__c = date.today();
          c.ChargentOrders__Gateway__c = 'a0QE0000003NfYtMAK';
          c.ChargentOrders__Currency__c = 'U.S. Dollar';
          c.Order_Item__c = 'Yearly Stem Cell Banking Fee';
          c.ChargentOrders__Payment_Frequency__c = 'Annual';
          c.ChargentOrders__Payment_Status__c = 'Recurring';
          c.ChargentOrders__Payment_Stop__c = 'Unending';
//          c.ChargentOrders__Payment_Start_Date__c = date.today().addYears(1);
//            c.ChargentOrders__Payment_Start_Date__c = scb.Stem_Cells_Banked_Date__c.addYears(1);
//          c.ChargentOrders__Payment_Start_Date__c = scb.Stem_Cells_Banked_Date__c;
      
      if(scb.Stem_Cells_Banked_Date__c!=null)
//        c.ChargentOrders__Payment_Start_Date__c = scb.Stem_Cells_Banked_Date__c.addYears(1);
        c.ChargentOrders__Payment_Start_Date__c = scb.Stem_Cells_Banked_Date__c;
      else
        scb.addError('Cannot have empty stem cell bank date before stage is set to stem cell banked');
          
      //add this new object to the list that would be inserted later
      co.add(c);
    } //end if
  } //end for scb
  
  //once the loop is done, insert the new record to Order
  try{
    insert co;
  } catch (system.Dmlexception e) {
    system.debug(e);
  }

}

 

Can the problem reside in here? I only have 3 more triggers, but they're pretty basic
 
Thank you!
hitesh90hitesh90

Is there any other trigger on Task Object which is on after Insert or before Insert?