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
betterbhushanbetterbhushan 

System.LimitException: Too many SOQL queries: 101

I am trying to get count of all Accounts which includes certain keyword present (multi-pick list).  But I get the following error:

 

System.LimitException: Too many SOQL queries: 101

 

I'm fairly new to Apex code.  The problem is that I am running SOQL inside a FOR loop, but I'm not quite sure how to resolve it. Apex Code is as below.  Any help would be appreciated.  Thanks.

 

 

public String getType() {
return type;
}
public void setType(String type){
this.type= type;
}

public Integer getTotal() {
return total;
}
public void setTotal(Integer total){
this.total= total;
}

public Integer getValid() {
return valid;
}
public void setValid(Integer valid){
this.valid= valid;
}

}

 

 

public List<phonesterDat> getCompanyData() {
List<phonesterDat> var1 = new List<phonesterDat>();
for (Keyword__c keyword : [select kw__c from Keyword__c]) {
phonesterDat var = new phonesterDat();
String str = keyword.kw__c;
var.setType(str);
var.setTotal([select count() from Account where DD_Segment__c includes (:str)]);
var1.add(var);
}
return var1;
}

 

If you want to whole code base. It is available here http://pastie.org/5151131

@anilbathula@@anilbathula@

Hi betterbhushan

 

The problem is you are writing the soql queries with in the for loop .

This cause the error ,for better practice write the soql queries outside the for loop;

http://wiki.developerforce.com/page/Best_Practice:_Avoid_SOQL_Queries_Inside_FOR_Loops

 

Thanks

Anil.B

jreehjreeh

The only way I can think of is to query the Data from Account ONCE before the FOR loop.

Then within in the for loop, manually use apex logic to count the record that includes "str".