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
plapla 

Too many SOQL queries: 101

Hello,

I wrote a trigger and a test class. My own test class passed. However there's an error thrown out from the other test class which has nothing to do with my trigger. Why it happened like that? Below is the error message. Please advise.

 

System.LimitException: Too many SOQL queries: 101

 

Thanks

Paul

 

 

 

Tim BarsottiTim Barsotti

Triggers fire in random order. It could be that your trigger used 99 queries and the next used 2 causing this error. 

 

I would look through and see where your using the most queries. Ensure all code is up to bulkified standards.

 

Also, might want to check to see if the test classes are using Test.StartTest / Test.StopTest methods to reset governor limits after the test data has been created.

plapla

Thanks for your reply.

 

Yes the other test class used Test.StartTest / Test.StopTest. So what does it mean to my case here?

 

Thanks

Paul

 

Tim BarsottiTim Barsotti

I would turn on debugging and see if you can determine what is querying over 100 times.

Tim BarsottiTim Barsotti

Would you post your new trigger? I'd like to just see if it is bulkified.

plapla

Here you go. Thanks for your help.

 

trigger

CaseClientTrigger onCase (beforeinsert, beforeupdate) {

String Clients;

// String CarrierId;

String GroupNo;

for(Case CaseUpdated:trigger.new){

 

if (CaseUpdated.AccountId != null){

 

// CarrierId = CaseUpdated.Carrier__c;

GroupNo = CaseUpdated.Group__c;

if((Trigger.ISINSERT) || (Trigger.ISUPDATE)){

 

Account accName = [select id, parentId, name, Type fromAccountwhereId =: CaseUpdated.AccountId];

 

// Clients = accName.Name;if (accName.Type == 'Client'){

Clients = accName.id;

}

if (accName.Type == 'Carrier'){

 

if (accName.parentId != null){

List<

Account> accName1 = [select id, parentId, name, Type fromAccountwhereId =: accName.parentId];

 

// Clients = accName1[0].Name;

Clients = accName1[0].id;

}

else{

Clients =

null;

}

}

if (accName.Type == 'Account'){

 

if (accName.parentId != null){

List<

Account> accName1 = [select id, parentId, name, Type fromAccountwhereId =: accName.parentId];

 

// Clients = accName1[0].Name; if (accName1[0].parentId != null){

List<

Account> accName2 = [select id, parentId, name, Type fromAccountwhereId =: accName1[0].parentId];

 

// Clients = Clients + '; ' + accName2[0].Name;

Clients = accName2[0].id;

}

else{

Clients =

null;

}

}

}

if (accName.Type == 'Group'){

 

if (accName.parentId != null){

List<

Account> accName1 = [select id, parentId, name, Type fromAccountwhereId =: accName.parentId];

 

// Clients = accName1[0].Name;if (accName1[0].parentId != null){

List<

Account> accName2 = [select id, parentId, name, Type fromAccountwhereId =: accName1[0].parentId];

 

// Clients = Clients + '; ' + accName2[0].Name;if (accName2[0].parentId != null){

List<

Account> accName3 = [select id, parentId, name, Type fromAccountwhereId =: accName2[0].parentId];

 

// Clients = Clients + '; ' + accName3[0].Name;

Clients = accName3[0].id;

}

else{

Clients =

null;

}

}

 

}

 

}

// Type = Group // System.debug('CaseClientTrigger/Client: ' + Clients);if (Clients != null){

CaseUpdated.Client__c = Clients;

}

else{

 

// List<Account> aName = [SELECT id, name, Type FROM Account WHERE Carrier_ID__c =: CarrierId and Type =: 'Client' and RecordTypeId =: '01230000000XhdNAAS' limit 1];

List<

Account> aName = [SELECT id, name, Type FROMAccountWHERE Group_Number__c =: GroupNo and Type =: 'Client'and RecordTypeId =: '01230000000XhdNAAS'limit1];

 

if(aName.size() > 0){

CaseUpdated.Client__c = aName[0].id;

}

else{

CaseUpdated.Client__c =

null;

}

}

 

}

// Trigger insert/update

 

}

// if accountId

 

}

// for structure

}

Tim BarsottiTim Barsotti

This trigger needs to be bulkified properly. Having the SOQL queries like this: "Account accName = [select id, parentId, name, Type fromAccountwhereId =: CaseUpdated.AccountId];" inside the for loop means it will query for each record being updated (up to 200). You need to get all of the queries outside of the loop as SFDC limits to 100 queries per execution context.

 

Please review bulkify principles and rework as needed. http://wiki.developerforce.com/page/Best_Practice%3A_Bulkify_Your_Code

Raj_Raj_

please do not use SOQL in for loop. 

 

 

or(Case CaseUpdated:trigger.new){

 

if (CaseUpdated.AccountId != null){

 

// CarrierId = CaseUpdated.Carrier__c;

GroupNo = CaseUpdated.Group__c;

if((Trigger.ISINSERT) || (Trigger.ISUPDATE)){

 

Account accName = [select id, parentId, name, Type fromAccountwhereId =: CaseUpdated.AccountId];

 

// Clients = accName.Name;if (accName.Type == 'Client'){

Clients = accName.id;

}

 

 

 

change the code like this 

 

or(Case CaseUpdated:trigger.new){

 

if (CaseUpdated.AccountId != null){

 

// CarrierId = CaseUpdated.Carrier__c;

GroupNo = CaseUpdated.Group__c;

if((Trigger.ISINSERT) || (Trigger.ISUPDATE)){

 

// Add the CaseUpdated.AccountId to a set and use the set to soql

// eg: (set<ofCaseUpdated.AccountId>)

 

// Clients = accName.Name;if (accName.Type == 'Client'){

Clients = accName.id;

}

 

Account accName = [select id, parentId, name, Type fromAccountwhereId in : (set<ofCaseUpdated.AccountId>)];

 

please do  bulkifi or you will hit governor limits which might cause these kind of issues. 

 

Thanks

Raj