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

MagulanDuraipandianMagulanDuraipandian

Avoid using SOQL error inside the for loop.

 

Regards,

Magulan D

Salesforce.com certified Force.com Developer.

SFDC Blog

If this post is your solution, kindly mark this as the solution.

Chamil MadusankaChamil Madusanka

Refer  this article : http://wiki.developerforce.com/page/Apex_Code_Best_Practices

 

If a reply to a post answers your question or resolves your problem, please mark it as the solution to the post so that others may benefit.