You need to sign in to do that
Don't have an account?
John Gerhard
to many soql queries: 101 JG
Hey guys, I am running into an issue with too many SOQL queries when I try a bulk test of my code. The weird part is I run into the issue when I have 25 or more pricebook entries. I'd imagine it is something little.
trigger ProductsCreatedonOpp on Opportunity (after insert) { //Initiate a list of strings for the SOQL to pull all the products we need List<String> oppsStringList = new List<String>(); //Initiate a list that will hold all of the line items we will need to insert List<OpportunityLineitem> lines = new List<OpportunityLineItem>(); //Fill the oppsStringList with the base quote template we want to filter the SOQL with for (Opportunity opps : Trigger.New){ oppsStringList.add(opps.Base_Quote_Tempalte__c); } System.debug('1'); //Build an array with all of the products we want to add to our opportunity List<PriceBookEntry> priceBookList = [SELECT Name,Id,unitPrice FROM PricebookEntry WHERE Quote_Template_Type__c IN:oppsStringList]; System.debug('2'); System.debug(priceBookList.size()); for(PricebookEntry priceBookEntries : priceBookList) { System.debug(priceBookEntries.Id); } System.debug('3'); //Run through each opportunity and then add each product to the lines list to insert for(Opportunity opps: Trigger.new){ for (PricebookEntry productsToAdd : priceBookList){ lines.add(new OpportunityLineItem(PriceBookEntryID=productsToAdd.Id, OpportunityID=opps.Id, UnitPrice=productsToAdd.UnitPrice, Quantity=opps.of_Screens_per_Month__c)); } } System.debug(lines); //Insert all of the products into the Opportunities insert lines; }
Your trigger is consuming only one SOQL. I guess the code running prioir to After Insert is consuming 100 SOQLs the SOQL in the above code is 101st SOQL resulting in exception.
Try to add this debug line at the start and end of the afterInsert trigger and see how many SOQLs are executed before entering into AfterInsert trigger
All Answers
Your trigger is consuming only one SOQL. I guess the code running prioir to After Insert is consuming 100 SOQLs the SOQL in the above code is 101st SOQL resulting in exception.
Try to add this debug line at the start and end of the afterInsert trigger and see how many SOQLs are executed before entering into AfterInsert trigger
I have a utility method you can use it to print many of the limits.
I also have one more method which actually check how close I am to limit. You can use this to send notification when the limits are reaching certain % (allowedLimit)