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

YET ANOTHER Too many SOQL queries: 21

I've been looking through exisiting issues with gonernors limit and I am puzzled at how I can change my current trigger on lead.


My custom made checkDups lead checks to see if Lead exists based on incoming leads email address. If count is not 0, it marks the incoming leads' status with Open - Duplicate.


Find below the code that is causing "Too many SOQL queries: 12" error


trigger CheckDups on Lead (before insert, after insert) {    List<DocDownload__c> toAdd = new List<DocDownload__c>();    if (Trigger.isBefore) {        for (Lead {            Integer dupCnt=[Select count( ) from Lead where];            if (dupCnt > 0) {                newLead.Status = 'Open - Duplicate';            }            }    }        if (Trigger.isAfter) {        String displayDate = '';        for (Lead {            if (newAfterLead.LeadType__c == 'DOWNLOAD') {                Datetime myDate =;                displayDate = myDate.format('MM-dd-yyyy hh:mm aaa z');                toAdd.add(new DocDownload__c(DocID__c=newAfterLead.DocID__c,DocTitle__c=newAfterLead.DocTitle__c,Email__c=newAfterLead.Email, DownloadDateTime__c=myDate, Title__c=newAfterLead.Title, Company__c=newAfterLead.Company, DisplayDate__c=displayDate, IsManual__c=false));             }        }                insert toAdd;    } 



What I'm puzzled about is this.

1. Is trigger wating until more than 20 records are collected before firing? This doesn't to be the case cuz every new lead that comes in from our website, it gets processed right away. The for loop that i have under before insert can not exceed 20 calls unless the trigger waits until more then 20 leads are collected.


2. How can i run bulk process for this? I need to check to see if each new lead has existing value in the system. Wouldn't i need to run for loop? 


The query in the loop is likely the problem.  Triggers fire upon save, whether it's 1 record or a batch of 200.  Yours will definitely fail if you do an import or mass edit of more than 20 leads. 


I believe the limits are shared across all triggers & apex as well, so you may have other triggers that are contributing to the problem.


To check dupes, something like this might scale better:


I'm sure others can better refine the loops to prevent statement limitation (10,000 limit) for large batches, but this should be a good start.


So the trigger actually fires when Leads are saved.

Alot of our leads are coming from our website through web2leads

Is there batch processing in place where incoming web2leads are collected until it actually saves?


I've modified the trigger so that it does the following.

Please provide feedback if you can.


I first collect all items in trigger new collection into a List.

I also collect List<String> of trigger news' email address

I  do a sql query to get list of existingEmail addresses using "in List<String>"


I than loop through collection of trigger new List and compare value of email address.


This SHOULD in theory fire sql query once correct? 


I don't believe W2L batches, which helps keep you in your limits.   


Your trigger logic sounds good - prossibly more effective than mine as well.  


Another way to make the code more efficient:

- Loop through list of existing leads 

- Add email address to a set (which avoids dupes)

- Loop through new leads, checking if the set contains the email address

- Surface error if so


yes - query should only fire one query.  Create a debug log to confirm (setup--> monitoring-->debug logs-->new)

Personally, I would recommend putting the email addresses for the new leads into set, use a map to store the results of the SOQL query that looks for any existing leads that match the new leads based on the set, and then loop through the new leads again, and use the contains method of the a map when you loop through the new leads to see they already exist.