You need to sign in to do that
Don't have an account?
daniel1110
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(); } }
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.
Hi Arun_lister, that's very helpful insight. I have about 6 triggers that does some variations of the the following:
Do you think the problem is here?
Thank you
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;
}
Hi! I tried your suggestion, but it didn't work... :(
Is there any other trigger on Task Object which is on after Insert or before Insert?