I am getting this error though limit 100 is provided in SOQL statement.Can someone please suggest a way out here? Apex code:

public  class Attachments_11{
public List<attachmentCaseWrapper> caseWrapper= new List<attachmentCaseWrapper>();
public void Attachments_11(){}

public List<Attachment> getattachedFiles() {
        List<Attachment> attachedFiles = [select ParentId,ID,Parent.Name from Attachment limit 10];
       return attachedFiles ;  

public List<attachmentCaseWrapper> getcaseWrapper(){
LIST <ID> K=new List<Id>();
for(attachment a :[SELECT PARENTID FRom attachment where Parent.recordtype.sobjecttype='Account'])

//List<Call2_vod__c> listCases = new List<Call2_vod__c>([select Account_vod__c,name,Account_vod__r.Name,id from Call2_vod__c where signature_date_vod__c!=Null and account_vod__C in :k limit 10 ]);
 //List<Id> hcp=new List<Id>();
 list<list<Call2_vod__c>> listOfLists = new list<list<Call2_vod__c>>();
list<Call2_vod__c> listOfContacts = new list<Call2_vod__c>();
for (Call2_vod__c c : [select Account_vod__c,name,Account_vod__r.Name,id from Call2_vod__c where signature_date_vod__c!=Null and account_vod__C in :k limit 100] )
    if (listOfContacts.size() > 999)
        listOfContacts = new list<Call2_vod__c>();


//and the last set...

for (list<Call2_vod__c> cons : listOfLists)
    for (Call2_vod__c con : cons)

 for (Attachment atf : [Select Id,Name,ParentId from attachment where parentId =:con.Account_vod__c AND Parent.recordtype.sobjecttype='Account' limit 1]) {
      map<id,Attachment> mapCaseAttachments = new map<id,attachment>();
         System.debug('keys in the map: ' + mapCaseAttachments.keySet());
            System.debug('list of values in the map : ' + mapCaseAttachments.values());
/*for(Call2_vod__C c : listCases )

     map<id,Attachment> mapCaseAttachments = new map<id,attachment>();
        for (List<Call2_vod__c> cs : listOfLists) {
               for (Call2_vod__c c : cs)    {
      attachment atf = mapCaseAttachments.get(c.Account_vod__C);

          attachmentCaseWrapper cw = new attachmentCaseWrapper(c,atf);


   // for2

     return caseWrapper;  


  public class attachmentCaseWrapper {
    public string Call_Name     {get; set;}
    public string accountName    {get; set;}
    public string attachmentId   {get; set;}

    public attachmentCaseWrapper (Call2_vod__c cse, Attachment atf) {
      Call_Name = cse.Name;
      accountName = cse.Account_vod__r.Name;
      if (atf != null) {
        attachmentId = atf.id;
        System.debug('***************************************************attachmentId : ' + atf.id);


Apex Page:

<apex:page controller="Attachments_11" renderas="pdf" >
  <apex:form id="theForm">
<apex:pageBlock id="details">

<apex:pageBlockTable value="{!caseWrapper}" var="j">        
         <apex:column value="{!j.accountName}"/>         
         <apex:column >
         <img src="/servlet/servlet.FileDownload?file={!j.attachmentId}"/>
         <!--apex:image url="/servlet/servlet.FileDownload?file={!j.attachmentId}"/-->


Error: Too many SOQL Queries 101

  • Hope you aware of governor limits in Salesforce. You have SOQLs inside a loop under for (list<Call2_vod__c> cons : listOfLists) {} try to optimize your code as much as possible
    – highfive
    Sep 11, 2014 at 10:45

2 Answers 2


You are misunderstanding the error message

  • SFDC is telling you that you are executing the SOQL Select statement more than 100 times in the course of the transaction

The statement you are referencing in your post title:

    [select Account_vod__c,name,Account_vod__r.Name,id from Call2_vod__c 
         where signature_date_vod__c!=Null and account_vod__C in :k 
         limit 100]

will only return 100 rows (max) per SOQL call. The number of rows returned is covered by different limits. See the Limits documentation.

As others have pointed out already; you can't put SOQL calls inside of for loops as when processing a batch of records will almost always lead to exceeding the SFDC SOQL governor limit.


Simple answer? Don't have a For loop that directly makes a Soql query within a for loop that also makes a query.

Not the answer you're looking for? Browse other questions tagged .