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
SwarnaSwarna 

Too many SOQL queries: 101

When and why do I get the following VisualForce error?

Too many SOQL queries: 101

 


SwarnaSwarna
This is the code in my Controller. I get the VISUALFORCE Error: Too many SOQL queries:101 sometimes.



Extranet_User_Mapping__c[] existingLeads = [Select Leads__c from Extranet_User_Mapping__c where Extranet_id__c =:extranet_id];

for(Extranet_User_Mapping__c leadId: existingLeads)
{

    Integer count = [select count() from Lead where id =:leadId.Leads__c];
    if(count != 0)
    {
        Lead selectedLead = [select id,firstname,lastname,email,phone from Lead where id =:leadId.Leads__c];
        CustomUser user = new CustomUser();
        user.setUserId(selectedLead.id);

mtbclimbermtbclimber
Becuase you your lead query is returning more than 50 rows and you are doing TWO queries inside the loop through which you are iterating over the results of your lead query.

Aside from any code changes I have a suspicion there are better ways to attack your problem.  Can you describe your use case/requirement?
SwarnaSwarna
I have stored the lead ids(Leads__c) in a custom table(Extranet_User_Mapping__c).
My requirement is to display the details of those lead ids.

The first query retrieves the ids.
The second query checks if the id exists in the Lead table & the third query gets the particulars of that lead id.
mtbclimbermtbclimber
Right. So why don't you create a lookup from lead to Extranet_User_Mapping__c and then use the standard controlller on Extranet_User_Mapping__c to output the collection of related leads using a binding like this:

{!Extranet_User_Mapping__c.leads__r}

If you don't need to do any filtering you could get away with no apex here.
SwarnaSwarna
After testing several cases, I came to the finding that if a query returns more than 10 or 15 records, I get the
Visualforce error: Too many SOQL queries: 101

In this method, I have a single query (without any loop), and still I get the error.

public
Lead[] getSelectedLeads()
{
    Lead[] allSelectedUsers = [select firstname,lastname from Lead where email in :listOfSuccessfulUsers];
    return allSelectedUsers;
}


jwetzlerjwetzler
where is getSelectedLeads being called from, and how many times?  If you lazy load it does it still happen?

Code:
Lead[] allSelectedUsers = null;

public Lead[] getSelectedLeads()
{
    if (allSelectedUsers == null) {
      allSelectedUsers = [select firstname,lastname from Lead where email in :listOfSuccessfulUsers];
    }
    return allSelectedUsers;
}

 

SwarnaSwarna
I call this method from apex page. And is called only once. This happens when more than 12 records are fetched.
mtbclimbermtbclimber
Please post your complete controller (any backing classes) and your page so we can investigate (and use the SRC button in the forum editor).

Thanks,