+ Start a Discussion
VFVF 

Too many SOQL queries: 101 how can we avoid this exception

Hello,

I am using a soql query to retrieve records from contact.

Here is my query:

List<contact> contactList=[Select name,Email from contact where id=:contactId];

 

I am able to execute this if i have less records but if i have more records that is fr about 200 Records i am getting an exception:

Too many SOQL queries: 101 .

 

When i looked for this Exception  i came to know that a database statement cannot process more than 100 records. 

 

Is there any method to overcome this issue please help me out with code if any.

 

Please help me out of this issue.

 

Thanks in advance,

prashanth. 

 

 

bbrantly1bbrantly1

Hello VF,

 

Can you post your whole controller code?  I'd be happy to help out.

jgrenfelljgrenfell

Actually, the error you're receiving is indicating that your running too many queries, and not referring to the number of rows returned.  Since the SoQL you show can only return 1 record at a time, I'm guessing you're calling it from inside of a loop so it gets executed multiple times.  You'll need to take that query out of the loop and retrieve all of the Contact records you need to process at one time using a where clause with IN, i.e. "where id IN:contactIds" (where contactIds is a set of Ids).  Depending on what you're trying to accomplish, you might want to put those Contacts into a Map so they can be retrieved by Id. 

 

Hope that helps, but if not, post the full code.

wesnoltewesnolte

Hey

 

You are using this query within a loop right? If this is the case you need to change your code. An example of a solution can be found here.

 

If you're having trouble adapting your code drop me a mail and I'll help you. 

 

Cheers,

Wes 

 

 

VFVF

Thanks a lot guyz....the idea worked i had a  Query inside the for loop.

I placed it outside the loop and made use of set ids .

Every thing working fine now .

Thanks once again....:smileyhappy:

JIDzenpriseJIDzenprise

I'm getting this error. If someone could help me bulkify this that would be AMAZING

 

trigger UpdateAccountPartner on Opportunity (before update) {
// THIS TRIGGER WILL OVERWRITE ANY PARTNER DEFINED IN THE FIELD Partner ON THE ACCOUNT OBJECT.
// SET THIS FIELD TO READ ONLY OR CHANGE THE FUNCTIONALITY BELOW TO AVOID DATA BEING OVERWRITTEN BY MISTAKE...


   for (Opportunity o : Trigger.new) {
  

 


       // CREATE ARRAY OF ALL PARTNERS ON THIS OPPORTUNITY. THE REASON WHY WE DONT PICK THE PRIMARY PARTNER ONLY
       // IS BECAUSE THE PRIMARY FLAG IS NOT ALWAYS SET WHEN PARTNERS ARE ADDED TO OPPORTUNITIES. ONLY WHEN SALES DOES TIS
       // MANUALLY FROM THE RELATED LIST IS PRIMARY CHECKBOX CHECKED...


       OpportunityPartner[] PartnerArray = 
       [select AccountToID, IsPrimary from OpportunityPartner where OpportunityId = :o.id ORDER BY isPrimary DESC, CreatedDate];
       if (PartnerArray.size() > 0) {
         
           // IF PRIMARY IS DEFINED THEN THIS WILL BE THE FIRST OBJECT. IF NOT THE FIRST ADDED PARTNER WILL BE ADDED...
                     List<Account> AccountArray = [select id,Partner_Account__c from Account where Id = :o.AccountId ];
           for (Account a: AccountArray ){
           
           //IF ACCOUNT PARTNER IS NOT THE PARTNER ON THE ACCOUNT AND THE PARTNER ON THE ACCOUNT IS NOT THE OPPORTUNITY ACCOUNT WHICH IT SHOULDN'T BE...
           if(a.Partner_Account__c!=PartnerArray[0].AccountToID&&a.Partner_Account__c!=a.id){
                       a.Partner_Account__c=PartnerArray[0].AccountToID;
                        update a;
            }
                       }
            
       }
   }
 }

 

venkateswarluvenkateswarlu

public void entryexitUpload(){
nameFile=contentFile.toString();
filelines = nameFile.split('\n');

Map<Integer,Entry_Exit_Criteria_setup__c> eec=new Map<Integer,Entry_Exit_Criteria_setup__c>();
Entry_Exit_Criteria_setup__c neweec;

for (Integer n=1;n<filelines.size();n++){
String[] inputvalues = new String[]{};
inputvalues = filelines[n].split(',');

Entry_Exit_Criteria_setup__c entryexit= new Entry_Exit_Criteria_setup__c();

entryexit.Name=inputvalues[2];
entryexit.Criteria_Description__c=inputvalues[8];
entryexit.Enabled_Flag__c=Boolean.valueOf(inputvalues[9]);
entryexit.Criteria_Type__c=inputvalues[10];

try{
Level2__c level2Records=[SELECT Id,Name FROM Level2__c where Name=:inputvalues[12].trim() and Level2__c.Level1__r.name =:inputvalues[14].trim()];
entryexit.Level2__c=level2Records.Id;
}catch(Exception e){}

eec.put(n,entryexit);
neweec=eec.get(n);

insert neweec;

entryexitStatus=' '+(filelines.size()-1)+' Records Uploaded Successfully';
statusColor='green';
}

}