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
Richard GilchristRichard Gilchrist 

Bulkified trigger, still getting 'Too Many SOQL queries'

Hi,

I have a trigger written as:
 
trigger UpdateLeadFunnelStage on CampaignMember (after insert)
{
    Set<Id> LeadIds = new Set<ID>();
    for(CampaignMember cm : Trigger.New) {
        LeadIds.add(cm.LeadId);
    }

    List<Lead> leads = [ SELECT Id, Lead_Funnel_Stage__c FROM Lead WHERE Id IN :LeadIds];
    List<Lead> leadsToUpdate = new List<Lead>();

    for(Lead l : leads) 
    {
        if(l.Lead_Funnel_Stage__c == null || l.Lead_Funnel_Stage__c == '')
        {
            l.Lead_Funnel_Stage__c = 'Inquiry';
            leadsToUpdate.add(l);
        }
    }

    Update leadsToUpdate;
}

I am receiving 'Too Many SOQL queries: 101' exceptions. I don't see how that's possible as this is bulkified and should only make one SOQL query per batch. Any ideas?
NiketNiket
I am wondering that you have trigger on Lead which might causing the issue. while you are updating leads in UpdateLeadFunnelStage trigger, that might executing the Lead trigger. All queries count for a context , no matter they are written in different triggers / classes. 

Have a look here for more detail : https://help.salesforce.com/apex/HTViewSolution?urlname=System-LimitException-Too-many-SOQL-queries-101&language=en_US 

Let me know if you need more help.


Please mark it as the solution if it answers your question so that others can also take benifit. 
 
Richard GilchristRichard Gilchrist
Hi Niket,

Thanks for the suggestion. I checked the Lead triggers and all of them are similarly bulkified, so they should only create one SOQL query per batch. There are some Lead triggers which are part of managed code, so I can't check those, but I'd hope they follow best practices.