+ Start a Discussion
JamesSSJamesSS 

How to avoid the 101 soql queries?

I have wrote some code for my page.

 

My page have  500 records.Each record different field values.I want to show the count of each record basis of field values.

 

So On page load, In controller I have  iterate each record and build two(values have taken from 2 different objects) dynamic soql and get the result count.But it exceed 50 th record it throws error(Query exception 101 soql queries).

 

How to avoid this in page load?Its urgent

 

 

 

 

Vadim RudkovVadim Rudkov

I think the problem is you put a soql query inside a loop. Could you provide some details of your controller?

Deepak Kumar ShyoranDeepak Kumar Shyoran

Hi James,

 

You are getting this exception because salesforce have a limit of 100 SOQL query in a single thread. If you are trying to execute more then 100 SOQl in a single context then salesforce shows this exception.

 

This exception normally appears when you trying to execute  a SOQL in a loop means if you have more then more then 100 records for that loop then it execute your SOQL more then 100 times which interns throws the exception. Please remove the SOQL from loop to avoid this exception.

 

Please let us know if you need more help.

 

If this post helps you then hit kudus by clicking on star and accept my post as a solution to your question.

sandeep@Salesforcesandeep@Salesforce

This is a governor limit defined by Salesforce to make load lighter on Server and increase server response.  Currently this is 100 allowed in single execution.

 

In your case what is happeing is like below 

 

for ( obj o : listofobjRecord) 

{

 

// Building Dynamic query 1st

Database.query(') ;

 

// Building Dynamic Query 2nd

Database.query();

 

}

 

so in each iteration it consumes 2 SOQL and in 50th iteration is touches 100 and after that is crossed the limit and fires Exception. SOQL 101 Toom many queries

 

So please do not use SOQL in LOOP and  try to put out it applying some logic or using some collections like MAP or LIST

Yoganand GadekarYoganand Gadekar

Figure out if you have any Select query within for loop, if you have so if you are bound to hit the limit of allowed 100 soql queries.

 

Hit kudos and mark this as answer if it helps you out.