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
TehNrdTehNrd 

Too many SOQL queries but trigger is written in bulk

Below is the code for a trigger on leads. As far as I can tell it is written in bulk and should not have any issues but for some reason we are getting unhandled trigger exceptions for too may SOQL queries, sometimes as high as 80 and I can't figure it out. There is only one SOQL query and it is not in any type of loop.

Code:
trigger leadPromotion on Lead (before insert, before update) {

   //For all the leads inserted or updated capture the Source Code
   Set<String> sourceCodes = new Set<String>();
   for(Lead l : Trigger.new){
    sourceCodes.add(l.Source_Code__c); 
   }
   
   //Query all Campaign/Promotions that have the Source Codes on the leads
   Map<Id,Campaign> campaignMap = new Map<Id,Campaign>([select Id, Source_Code__c From Campaign where Source_Code__c IN :sourceCodes]);
 
   //Create and populated a Source Code -> CampaignID Map
   Map<String,ID> sourceCodeToCampaignMap = new Map<String,ID>();
   for(Campaign c : campaignMap.values()){
    sourceCodeToCampaignMap.put(c.Source_Code__c,c.Id);
   }
   
   //For all of the leads being inserted or updated retrieve the Id of the corresponding Promotion and set it to the Primary Promotion field (lookup)
   for(Lead l : Trigger.new){
    if(l.Source_Code__c == null){ 
     l.Primary_Promotion__c = null; 
    }else{
     l.Primary_Promotion__c = sourceCodetoCampaignMap.get(l.Source_Code__c); 
    }
   }
}

I can insert 200 record through the data loader with no issues but when we have an external integration insert these leads an the error occurs.

mikefmikef
TehNed:

So the trigger looks good and from what I can see the trigger is not the issue.

I guess is the outside integration is causing another trigger to fire that is not built for bulk use.

If you want to change this you can:
Code:
 //Query all Campaign/Promotions that have the Source Codes on the leads
   Map<Id,Campaign> campaignMap = new Map<Id,Campaign>([select Id, Source_Code__c From Campaign where Source_Code__c IN :sourceCodes]);
 
   //Create and populated a Source Code -> CampaignID Map
   Map<String,ID> sourceCodeToCampaignMap = new Map<String,ID>();
   for(Campaign c : campaignMap.values()){
    sourceCodeToCampaignMap.put(c.Source_Code__c,c.Id);
   }
   
   //For all of the leads being inserted or updated retrieve the Id of the corresponding Promotion and set it to the Primary Promotion field (lookup)
   for(Lead l : Trigger.new){
    if(l.Source_Code__c == null){ 
     l.Primary_Promotion__c = null; 
    }else{
     l.Primary_Promotion__c = sourceCodetoCampaignMap.get(l.Source_Code__c); 
    }
   }

to this and it should work.


Code:
 //Query all Campaign/Promotions that have the Source Codes on the leads
   Map<String,Id> campaignMap = new Map<String,Id>();
 
   //Create and populated a Source Code -> CampaignID Map
   for(Campaign c :[select Id, Source_Code__c From Campaign where Source_Code__c IN :sourceCodes]){
    campaignMap.put(c.Source_Code__c,c.Id);
   }
   
   //For all of the leads being inserted or updated retrieve the Id of the corresponding Promotion and set it to the Primary Promotion field (lookup)
   for(Lead l : Trigger.new){
    if(l.Source_Code__c == null){ 
     l.Primary_Promotion__c = null; 
    }else{
     l.Primary_Promotion__c = campaignMap.get(l.Source_Code__c); 
    }
   }

 
That will save you a map but that is it.
Please post what the outside integration does.
 

TehNrdTehNrd
The outside integration either, inserts or updates leads.

I will try the code change but from what I've read that is the fix for a query that returns too many records, not necessarily to many SOQL calls.

I can't seem to figure out why that SOQL query is executing so many times. Even if 4000 records are inserted it should only execute once.

This trigger also doesn't update any other records that would cause additional triggers to fire.

Thanks for the help.
werewolfwerewolf
Did you turn on debug monitoring to see what it's doing?  Maybe some kind of cascading that's causing it to consider the single operation both an insert and an update?
TehNrdTehNrd
I'm working on getting some debug logs but the integration logs in numerous time in a day to perform its operations and sometimes everything works fine but the debug log only keeps a record of the last 20 operations so I'm having a hard time catching the bugs in the log as I need to constantly reset it.


Message Edited by TehNrd on 05-15-2008 08:44 AM
TehNrdTehNrd
I got the debug log. It looks like the trigger starts and ends but the SOQL query and row count is not reseting to 0.

This is only part of it. It goes all the way up to 50 SOQL queries and it is still climbing as I type this.

Code:
User iMarketing Automation Date 5/15/2008 9:46:01 AM PDT 
Status Too many SOQL queries: 50 Application 000000000003426
Request Type Application Operation Api
Duration (ms) 2,932 Log Length 94,277
Log *** Beginning leadPromotion on Lead trigger event BeforeInsert for null, null, null, null, null, null, null

20080515164558.826:Trigger.leadPromotion: line 6, column 13: SelectLoop:LIST:SOBJECT:Lead
20080515164558.826:Trigger.leadPromotion: line 11, column 65: SOQL query with 299 rows finished in 20 ms
20080515164558.826:Trigger.leadPromotion: line 15, column 13: SelectLoop:LIST:SOBJECT:Campaign
20080515164558.826:Trigger.leadPromotion: line 20, column 13: SelectLoop:LIST:SOBJECT:Lead
20080515164558.826:Trigger.leadPromotion: line 29, column 12: returning Boolean from method public static Boolean throwExceptionError() in 0 ms

Cumulative resource usage:

Resource usage for namespace: (default)
Number of SOQL queries: 1 out of 20
Number of query rows: 299 out of 7000
Number of SOSL queries: 0 out of 0
Number of DML statements: 0 out of 20
Number of DML rows: 0 out of 700
Number of script statements: 319 out of 11400
Maximum heap size: 0 out of 100000
Number of callouts: 0 out of 10
Number of Email Invocations: 0 out of 10

Total email recipients queued to be sent : 0

*** Ending leadPromotion on Lead trigger event BeforeInsert for null, null, null, null, null, null, null

*** Beginning leadPromotion on Lead trigger event BeforeUpdate for 00Q5000000LHoa3

20080515164559.871:Trigger.leadPromotion: line 6, column 13: SelectLoop:LIST:SOBJECT:Lead
20080515164559.871:Trigger.leadPromotion: line 11, column 65: SOQL query with 299 rows finished in 13 ms
20080515164559.871:Trigger.leadPromotion: line 15, column 13: SelectLoop:LIST:SOBJECT:Campaign
20080515164559.871:Trigger.leadPromotion: line 20, column 13: SelectLoop:LIST:SOBJECT:Lead
20080515164559.871:Trigger.leadPromotion: line 29, column 12: returning Boolean from method public static Boolean throwExceptionError() in 0 ms

Cumulative resource usage:

Resource usage for namespace: (default)
Number of SOQL queries: 2 out of 20
Number of query rows: 598 out of 7000
Number of SOSL queries: 0 out of 0
Number of DML statements: 0 out of 20
Number of DML rows: 0 out of 700
Number of script statements: 625 out of 11400
Maximum heap size: 0 out of 100000
Number of callouts: 0 out of 10
Number of Email Invocations: 0 out of 10

Total email recipients queued to be sent : 0

*** Ending leadPromotion on Lead trigger event BeforeUpdate for 00Q5000000LHoa3

*** Beginning leadPromotion on Lead trigger event BeforeUpdate for 00Q5000000LHoa4

20080515164559.997:Trigger.leadPromotion: line 6, column 13: SelectLoop:LIST:SOBJECT:Lead
20080515164559.997:Trigger.leadPromotion: line 11, column 65: SOQL query with 0 rows finished in 5 ms
20080515164559.997:Trigger.leadPromotion: line 15, column 13: SelectLoop:LIST:SOBJECT:Campaign
20080515164559.997:Trigger.leadPromotion: line 20, column 13: SelectLoop:LIST:SOBJECT:Lead
20080515164559.997:Trigger.leadPromotion: line 29, column 12: returning Boolean from method public static Boolean throwExceptionError() in 0 ms

Cumulative resource usage:

Resource usage for namespace: (default)
Number of SOQL queries: 3 out of 20
Number of query rows: 598 out of 7000
Number of SOSL queries: 0 out of 0
Number of DML statements: 0 out of 20
Number of DML rows: 0 out of 700
Number of script statements: 632 out of 11400
Maximum heap size: 0 out of 100000
Number of callouts: 0 out of 10
Number of Email Invocations: 0 out of 10

Total email recipients queued to be sent : 0

*** Ending leadPromotion on Lead trigger event BeforeUpdate for 00Q5000000LHoa4

*** Beginning leadPromotion on Lead trigger event BeforeUpdate for 00Q5000000LHoa5

20080515164600.059:Trigger.leadPromotion: line 6, column 13: SelectLoop:LIST:SOBJECT:Lead
20080515164600.059:Trigger.leadPromotion: line 11, column 65: SOQL query with 0 rows finished in 6 ms
20080515164600.059:Trigger.leadPromotion: line 15, column 13: SelectLoop:LIST:SOBJECT:Campaign
20080515164600.059:Trigger.leadPromotion: line 20, column 13: SelectLoop:LIST:SOBJECT:Lead
20080515164600.059:Trigger.leadPromotion: line 29, column 12: returning Boolean from method public static Boolean throwExceptionError() in 0 ms

Cumulative resource usage:

Resource usage for namespace: (default)
Number of SOQL queries: 4 out of 20
Number of query rows: 598 out of 7000
Number of SOSL queries: 0 out of 0
Number of DML statements: 0 out of 20
Number of DML rows: 0 out of 700
Number of script statements: 639 out of 11400
Maximum heap size: 0 out of 100000
Number of callouts: 0 out of 10
Number of Email Invocations: 0 out of 10

Total email recipients queued to be sent : 0

*** Ending leadPromotion on Lead trigger event BeforeUpdate for 00Q5000000LHoa5

*** Beginning leadPromotion on Lead trigger event BeforeUpdate for 00Q5000000LHoa6

20080515164600.113:Trigger.leadPromotion: line 6, column 13: SelectLoop:LIST:SOBJECT:Lead
20080515164600.113:Trigger.leadPromotion: line 11, column 65: SOQL query with 299 rows finished in 14 ms
20080515164600.113:Trigger.leadPromotion: line 15, column 13: SelectLoop:LIST:SOBJECT:Campaign
20080515164600.113:Trigger.leadPromotion: line 20, column 13: SelectLoop:LIST:SOBJECT:Lead
20080515164600.113:Trigger.leadPromotion: line 29, column 12: returning Boolean from method public static Boolean throwExceptionError() in 0 ms

Cumulative resource usage:

Resource usage for namespace: (default)
Number of SOQL queries: 5 out of 20
Number of query rows: 897 out of 7000
Number of SOSL queries: 0 out of 0
Number of DML statements: 0 out of 20
Number of DML rows: 0 out of 700
Number of script statements: 945 out of 11400
Maximum heap size: 0 out of 100000
Number of callouts: 0 out of 10
Number of Email Invocations: 0 out of 10

Total email recipients queued to be sent : 0

*** Ending leadPromotion on Lead trigger event BeforeUpdate for 00Q5000000LHoa6

*** Beginning leadPromotion on Lead trigger event BeforeUpdate for 00Q5000000LHoa7

20080515164600.209:Trigger.leadPromotion: line 6, column 13: SelectLoop:LIST:SOBJECT:Lead
20080515164600.209:Trigger.leadPromotion: line 11, column 65: SOQL query with 0 rows finished in 5 ms
20080515164600.209:Trigger.leadPromotion: line 15, column 13: SelectLoop:LIST:SOBJECT:Campaign
20080515164600.209:Trigger.leadPromotion: line 20, column 13: SelectLoop:LIST:SOBJECT:Lead
20080515164600.209:Trigger.leadPromotion: line 29, column 12: returning Boolean from method public static Boolean throwExceptionError() in 0 ms

Cumulative resource usage:

Resource usage for namespace: (default)
Number of SOQL queries: 6 out of 20
Number of query rows: 897 out of 7000
Number of SOSL queries: 0 out of 0
Number of DML statements: 0 out of 20
Number of DML rows: 0 out of 700
Number of script statements: 952 out of 11400
Maximum heap size: 0 out of 100000
Number of callouts: 0 out of 10
Number of Email Invocations: 0 out of 10

Total email recipients queued to be sent : 0

*** Ending leadPromotion on Lead trigger event BeforeUpdate for 00Q5000000LHoa7

*** Beginning leadPromotion on Lead trigger event BeforeUpdate for 00Q5000000LHoa8

20080515164600.295:Trigger.leadPromotion: line 6, column 13: SelectLoop:LIST:SOBJECT:Lead
20080515164600.295:Trigger.leadPromotion: line 11, column 65: SOQL query with 299 rows finished in 13 ms
20080515164600.295:Trigger.leadPromotion: line 15, column 13: SelectLoop:LIST:SOBJECT:Campaign
20080515164600.295:Trigger.leadPromotion: line 20, column 13: SelectLoop:LIST:SOBJECT:Lead
20080515164600.295:Trigger.leadPromotion: line 29, column 12: returning Boolean from method public static Boolean throwExceptionError() in 0 ms

Cumulative resource usage:

Resource usage for namespace: (default)
Number of SOQL queries: 7 out of 20
Number of query rows: 1196 out of 7000
Number of SOSL queries: 0 out of 0
Number of DML statements: 0 out of 20
Number of DML rows: 0 out of 700
Number of script statements: 1258 out of 11400
Maximum heap size: 14988 out of 100000
Number of callouts: 0 out of 10
Number of Email Invocations: 0 out of 10

Total email recipients queued to be sent : 0

*** Ending leadPromotion on Lead trigger event BeforeUpdate for 00Q5000000LHoa8

*** Beginning leadPromotion on Lead trigger event BeforeUpdate for 00Q5000000LHoa9

20080515164600.380:Trigger.leadPromotion: line 6, column 13: SelectLoop:LIST:SOBJECT:Lead
20080515164600.380:Trigger.leadPromotion: line 11, column 65: SOQL query with 0 rows finished in 6 ms
20080515164600.380:Trigger.leadPromotion: line 15, column 13: SelectLoop:LIST:SOBJECT:Campaign
20080515164600.380:Trigger.leadPromotion: line 20, column 13: SelectLoop:LIST:SOBJECT:Lead
20080515164600.380:Trigger.leadPromotion: line 29, column 12: returning Boolean from method public static Boolean throwExceptionError() in 0 ms

Cumulative resource usage:

Resource usage for namespace: (default)
Number of SOQL queries: 8 out of 20
Number of query rows: 1196 out of 7000
Number of SOSL queries: 0 out of 0
Number of DML statements: 0 out of 20
Number of DML rows: 0 out of 700
Number of script statements: 1265 out of 11400
Maximum heap size: 14988 out of 100000
Number of callouts: 0 out of 10
Number of Email Invocations: 0 out of 10

Total email recipients queued to be sent : 0

*** Ending leadPromotion on Lead trigger event BeforeUpdate for 00Q5000000LHoa9

*** Beginning leadPromotion on Lead trigger event BeforeUpdate for 00Q5000000LHoa3

20080515164600.438:Trigger.leadPromotion: line 6, column 13: SelectLoop:LIST:SOBJECT:Lead
20080515164600.438:Trigger.leadPromotion: line 11, column 65: SOQL query with 299 rows finished in 12 ms
20080515164600.438:Trigger.leadPromotion: line 15, column 13: SelectLoop:LIST:SOBJECT:Campaign
20080515164600.438:Trigger.leadPromotion: line 20, column 13: SelectLoop:LIST:SOBJECT:Lead
20080515164600.438:Trigger.leadPromotion: line 29, column 12: returning Boolean from method public static Boolean throwExceptionError() in 0 ms

Cumulative resource usage:

Resource usage for namespace: (default)
Number of SOQL queries: 9 out of 20
Number of query rows: 1495 out of 7000
Number of SOSL queries: 0 out of 0
Number of DML statements: 0 out of 20
Number of DML rows: 0 out of 700
Number of script statements: 1571 out of 11400
Maximum heap size: 14988 out of 100000
Number of callouts: 0 out of 10
Number of Email Invocations: 0 out of 10

Total email recipients queued to be sent : 0

*** Ending leadPromotion on Lead trigger event BeforeUpdate for 00Q5000000LHoa3

*** Beginning leadPromotion on Lead trigger event BeforeUpdate for 00Q5000000LHoa4

20080515164600.503:Trigger.leadPromotion: line 6, column 13: SelectLoop:LIST:SOBJECT:Lead
20080515164600.503:Trigger.leadPromotion: line 11, column 65: SOQL query with 0 rows finished in 5 ms
20080515164600.503:Trigger.leadPromotion: line 15, column 13: SelectLoop:LIST:SOBJECT:Campaign
20080515164600.503:Trigger.leadPromotion: line 20, column 13: SelectLoop:LIST:SOBJECT:Lead
20080515164600.503:Trigger.leadPromotion: line 29, column 12: returning Boolean from method public static Boolean throwExceptionError() in 0 ms

Cumulative resource usage:

Resource usage for namespace: (default)
Number of SOQL queries: 10 out of 20
Number of query rows: 1495 out of 7000
Number of SOSL queries: 0 out of 0
Number of DML statements: 0 out of 20
Number of DML rows: 0 out of 700
Number of script statements: 1578 out of 11400
Maximum heap size: 14988 out of 100000
Number of callouts: 0 out of 10
Number of Email Invocations: 0 out of 10

Total email recipients queued to be sent : 0

*** Ending leadPromotion on Lead trigger event BeforeUpdate for 00Q5000000LHoa4

*** Beginning leadPromotion on Lead trigger event BeforeUpdate for 00Q5000000LHoa5

20080515164600.542:Trigger.leadPromotion: line 6, column 13: SelectLoop:LIST:SOBJECT:Lead
20080515164600.542:Trigger.leadPromotion: line 11, column 65: SOQL query with 0 rows finished in 4 ms
20080515164600.542:Trigger.leadPromotion: line 15, column 13: SelectLoop:LIST:SOBJECT:Campaign
20080515164600.542:Trigger.leadPromotion: line 20, column 13: SelectLoop:LIST:SOBJECT:Lead
20080515164600.542:Trigger.leadPromotion: line 29, column 12: returning Boolean from method public static Boolean throwExceptionError() in 0 ms

Cumulative resource usage:

Resource usage for namespace: (default)
Number of SOQL queries: 11 out of 20 ******* CLOSE TO LIMIT
Number of query rows: 1495 out of 7000
Number of SOSL queries: 0 out of 0
Number of DML statements: 0 out of 20
Number of DML rows: 0 out of 700
Number of script statements: 1585 out of 11400
Maximum heap size: 14988 out of 100000
Number of callouts: 0 out of 10
Number of Email Invocations: 0 out of 10

Total email recipients queued to be sent : 0

*** Ending leadPromotion on Lead trigger event BeforeUpdate for 00Q5000000LHoa5

 



Message Edited by TehNrd on 05-15-2008 09:55 AM

Message Edited by TehNrd on 05-15-2008 10:24 AM
mikefmikef
Looks like your trigger is getting called over and over again.

And it looks like new Leads are being created and then the trigger is firing on them as well.

Can you post the full trigger?


TehNrdTehNrd
The whole trigger is in the very first post. I have not had time to make any changes.

One strange thing I have noticed is that for the first Id it tries to update, 00Q5000000LHoa3, when I try to look this this up in the UI is says the Data Not Available, yet it seems to be processing this record ok in the trigger.

It looks like it is inserting the leads and trying to update them at the same time. Looking at the first lines of the log you can see it inserts 7 records:

Log *** Beginning leadPromotion on Lead trigger event BeforeInsert for null, null, null, null, null, null, null

I have made the debug log above longer and you can see it repeatedly tries to process 7 Ids over and over again. This eventually completely errors out and the insert is reverted so it is not there when I look it up.

Interesting facts but it still doesn't answer why its looping over and over.


Message Edited by TehNrd on 05-15-2008 10:37 AM
Kevin_MKevin_M
Make sure you don't have any workflow rules that update the lead you just inserted.  If this is the case, the trigger will continue to fire each time the newly inserted lead is updated by the rule and the SOQL query count will not be reset to 0.
TehNrdTehNrd
I'm sort of bring this thread back from the dead but I tweaked the trigger a little but and I haven't seen this error since.

Code:
//For all the leads inserted or updated capture the Source Code
   //change 1: now I only add values to this set that have a value
Set<String> sourceCodes = new Set<String>(); for(Lead l : Trigger.new){ if(l.source_Code__c != null){ sourceCodes.add(l.Source_Code__c); } }
//change 2: put the SOQL query in the for loop. //Create and populated a Source Code -> CampaignID Map Map<String,Id> campaignMap = new Map<String,Id>(); for(Campaign c :[select Id, Source_Code__c From Campaign where Source_Code__c IN :sourceCodes]){ campaignMap.put(c.Source_Code__c,c.Id); } //For all of the leads being inserted or updated retrieve the Id of the corresponding Promotion and set it to the Primary Promotion field (lookup) for(Lead l : Trigger.new){ if(l.Source_Code__c == null){ l.Primary_Promotion__c = null; }else{ l.Primary_Promotion__c = campaignMap.get(l.Source_Code__c); } }