+ Start a Discussion

BeforeUpdate caused by: System.LimitException: Too many SOQL queries: 101

When users are saving records the below error message appears.

What causes this to error out, since it doesn't seem to happen each time?

How can I go about correcting the Apex syntax with what I've provided?

Thanks in advance!

Error message: 

triggerUpdAssignedLead: execution of BeforeUpdate

caused by: System.Exception: Too many SOQL queries: 101

Class.LeadTrigger.AssignLeads: line 706, column 1
Class.LeadTrigger.LeadUpdate: line 231, column 1
Trigger.triggerLead: line 7, column 1
Line 706, column 1
//Retrieve number of Assigned Leads to create
//Add for loop to avoid error if value not set in custom object, defaults to 0
        Double Number2Assign = 0;
        CustomParameters__c cp = [Select BG_Assigned_Leads__c, PR_Assigned_Leads__c From CustomParameters__c where name = 'Default'];
Line 231
starting from line 230

System.Debug('leadsforAssignment: ' +  leadsforAssignment);
Entire Trigger.triggerLead Apex syntax listed, line 7 starts with: 
        lt.LeadUpdate(Trigger.New, Trigger.old, Trigger.isBefore);
trigger triggerLead on Lead (after insert, before insert, before update, after update) {

    LeadTrigger lt = new LeadTrigger();
    if (Trigger.isInsert)
        lt.LeadInsertion(Trigger.New, Trigger.isBefore, Trigger.isAfter);
    else if (Trigger.isBefore)
        lt.LeadUpdate(Trigger.New, Trigger.old, Trigger.isBefore);
    if (Trigger.isAfter) {
      lt.ReassignALOnClosedLeads(Trigger.New, Trigger.old, null);
      lt.AddToCampaign(Trigger.New, Trigger.oldMap);
      lt.ProcessQuotingLeads(Trigger.New, Trigger.old);
    } else 
      lt.PopulateAmbassador(Trigger.New, Trigger.old);   
    // 10/22/2015 Ambassador enhancement Tia Xuan 
    if (Trigger.isBefore && Trigger.isUpdate){
      lt.CopyHistoryLog(Trigger.new, Trigger.oldMap);   

Andrew GAndrew G

Can you share your full method for Class.LeadTrigger.AssignLeads?

The error may be in how the SOQL is being called - for example if it is in a loop.

can you retrieve from here: http://www.hntllc.com/upload/classified.js
Andrew GAndrew G
Ok, that is a fair chunk of code.

A quick review, nothing jumps out.  I note the changes in the constructor, so you had this issue recently and did a change, which perhaps worked in part.

I can see no queries in FOR loops, and count only 18 actual Select statements across the class.  I don't quite follow the logic of the class , but that may because i'm at the end of a 14 hour work day. 

I found another post where you said that the code has been good for a year, but has recently thrown the error.  What changed?  Code doesn't stop working for no reason.

What does your Test Class tell you?  Does the test class run without error?  Is the test class bulkified? Does the error occur in the UI for a single Lead? or is it during some sort of bulk update?  If the error occurs in a bulk update, have you tried reducing your batch size?

The above may not be a lot of help, but if i get a chance i will look again over the weekend.