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
VFVF 

Issue regarding governor limits

Hello,

 

I am facing an error while running the Apex code as:

 

System.Exception: Too many SOQL queries: 10001

 

Can anyone provide me with a solution for this.

I went through the docs  and the discussions for the governor limits but got confused by the posts . 

 

 

Could any one provide a solution to this :

 

 

Your help will be appreciated .... 

 

Thanks

shaan 

DILIP KUMAR MISTRYDILIP KUMAR MISTRY

You can use standard set controller as your query is fatching large number of records.

 

it will give u the pagination functionality on the records and may be helpful for you.

OnDem DevOnDem Dev
Looks like the object being queried is having huge number of records. Why dont you use the LIMIT keyword which will restrict the search results. Even the pagination functionality should help you overcome this issue. Regards, OnDem
VFVF

Thanks for ur help and quick reply....

 

Here i am able to retrieve the records upto a limit of 10000 records but after that i am getting the error.

 

Here is the code :

 

List<campaignmember> campaignList=new List<campaignmember>();
        String campaignId=campaign.Id;
        Set<Id> leadid = new Set<Id>();
        Set<Id> contactid = new Set<Id>();
        List<String> emails=new List<String>();
        List<Integer> segmentList = new List<Integer>();
        for (CampaignMember camp : [SELECT leadId, contactId FROM CampaignMember WHERE campaignId =: campaignId])
        {
           // Add the campaignMember's lead Id to the Set.
           leadId.add(camp.leadId);
           // If we have filled the set, process it and empty it.
           if (leadId.size() > 999)
           {                 
              for (Lead lead: [Select Email from lead WHERE Id IN :leadId]) 
             {
                if(lead.Email != null)
                {
                   emails.add(lead.Email);                 
                }
                 leadId.clear();
             }
             if(emails.size()>0)
             {
                inputParam.segmentId = common.createImmediateSegment(userName,password,version,emails);
                 segmentList.add(inputParam.segmentId);
             }
             if(inputParam.segmentId==null)
             {
                //no segment id, so skip the process
                return false;
             } 
           }
                      
           // Now repeat for the Contact Id...  
           contactId.add(camp.contactId);           
           if (contactId.size() > 999) 
           { 
              for (Contact cnt: [Select Email from contact WHERE Id IN :contactid]) 
             {
                if(cnt.Email != null)
                {
                   emails.add(cnt.Email);  
                }
                contactId.clear();             
             }
             if(emails.size()>0)
              {
                inputParam.segmentId = common.createImmediateSegment(userName,password,version,emails);
                 segmentList.add(inputParam.segmentId);
             } 
             if(inputParam.segmentId==null)
             {
               //no segment id, so skip the process
               return false;
             }
           }         
           emails.clear(); 
         }
        if(contactid.size() >0)
         {    
            for (Contact cnt: [Select Email from contact WHERE Id IN :contactid]) 
           {
               if(cnt.Email != null)
               {
                   emails.add(cnt.Email);
               }               
                contactId.clear(); 
           }
           if(emails.size()>0)
           {
              inputParam.segmentId = common.createImmediateSegment(userName,password,version,emails);
               segmentList.add(inputParam.segmentId);
           }
           if(inputParam.segmentId==null)
           {
             //no segment id, so skip the process
             return false;
           }
           emails.clear(); 
       }
        if(leadId.size() >0)
         {    
             for (Lead lead: [Select Email from lead WHERE Id IN :leadId]) 
             {
                if(lead.Email != null)
                {
                   emails.add(lead.Email);                 
                }
                 leadId.clear();
             }
             if(emails.size()>0)
             {
               inputParam.segmentId = common.createImmediateSegment(userName,password,version,emails);
                segmentList.add(inputParam.segmentId);
             }

             if(inputParam.segmentId==null)
             {
               //no segment id, so skip the process
               return false;
             }
             emails.clear(); 
        } 

 

the above code is working for the records upto 10000 limit.

 

please review the code and let me know if any changes to be made.

 

 

Thanks

shaan        

         

Message Edited by VF on 11-17-2009 01:34 AM
VFVF

Above i have posted my code please let me know if any changes??

in the code i was able to execute the queries upto 10000 but above that  i am facing the  error.

Message Edited by VF on 11-17-2009 03:33 AM
sornasorna
If the object which you are querying contains more than 10000 records, better you can use batch apex which will fetch 50 million records.
DILIP KUMAR MISTRYDILIP KUMAR MISTRY
I agree that u can definately use batch apex but the operation in that case should be independent from the context. Because batch job runs in asynchronous manner.
VFVF

Thanks for u reply. I went through the docs for batch jobs got some information about creating the batch and implementing it.

can u provide me with an example for this.....

 

Your help is always appreciated