+ Start a Discussion

SOQL query governor limit reached while importing quote lines from text file

I wrote an apex controller and class to handle precessing a tab delimited text file that gets attached to a quote.  The file gets parsed into a list/array of string and the quotelines in the file get processed.  The quote line string contains a product code (manufacturer code), desctiption, manufacturer, price, quanity, and cost.  I am looping over the items in my list, parsing the line apart and attempting to create a new quoteline object, then adding all the individual line objects to a collection to get inserted.  It actually works great when the file coming in has less that 100 lines.  Once it is over 100 a governor limit is reached and I can no longer run my SOQL query inside my process method.  What is the best way to approach this.  I need to have the productId in order to insert a quote line?  I'm sure there is a more effeciant way but I'm drawing a blank.


Shothand version below please ignore sytax it's just to convey what I'm trying to do:



public boolean ProcessProductQuote()

    List<string> LinesToProcess = new List<string>();


    //add relevant quote lines to list****




    for (string QuoteLine: LinesToProcess)
          ProcessProductQuoteLine(QuoteLine, QuoteID);



Private void ProcessProductQuoteLine(string QuoteLine,string QuoteID)

         //get part number if it exists.  If nothing is returned for the part on this line a new product is added

         List<Product2> P = [select Id from Product2 where ProductCode = :PartNumber];


You've got your SOQL query for P embedded in a loop so it's executing repeatedly.
Create a map of the product2 items then use that in your loop instead of the query.

List<Product2> p2 = [SELECT id, productcode FROM product2];
Map<String,Id> ProdMap = new Map<String,Id>();
For (Product2 p:p2){

// then you can use ProdMap.get(productcode) to return the ID.