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
Padmaja GadepalliPadmaja Gadepalli 

Too many SOQL queries: 101- updating Quote

i am getting  "Too many SOQL queries: 101" issue when trying ot save a quote and only when the quote lineitems count exceeds 80.
Here is piece of code that apparently is causing issue .
cQuoteGroups = new List<SelectOption>();
            cQuoteGroups.add(new SelectOption('','--None--'));
                  lstQLG = [SELECT Id,Name FROM Quote_Line_Group__c WHERE Quote__c = :cQuote.Id Order By Order__c ASC];
            for(Quote_Line_Group__c qlg : lstQLG){
                if(qlg.Name != 'Change' && qlg.Name != 'Remove'){
                    cQuoteGroups.add(new SelectOption(qlg.Name,qlg.Name));
                                 }
            }
please advise..
ManojSankaranManojSankaran
Hi Padmaja,

We will normally get his error only if the total number of query exceeds the limit. You have to check whether there is any recussion happening in your code. or is there any query inside a for loop.


Remove Query inside a for loop
Avoid recurssion.
Remove unnecessary query from the code.


If possible post your entire code. So that it would be easy for us to identify the issue. If this really solves your query mark is as answer.


Thanks
Manoj S
Padmaja GadepalliPadmaja Gadepalli
hi Manoj , 
i made sure that there is no query inside forloop and not sure how to check for recurssion ..
here is teh code for the method that gives error of exceeding soql queries..
public PageReference saveAll(){
        PageReference prReturn = null;
        Savepoint sp = Database.setSavePoint();
        try{
            if(cQuote.Term__c == null){
                throw new DatapipeUtility.CustomException('Required fields are missing: [Term]');
            }
            Boolean isNew = cQuote.Id == null;
            List<JQGridItem> lstJQGI = (List<JQGridItem>)JSON.deserialize(strJSONQLGrid,List<JQGridItem>.class);
            Map<String,Id> mapDCNameIds = new Map<String,Id>();
            Map<String,Id> mapQGNameIds = new Map<String,Id>();
            Map<String,QuoteLineItem> mapGridIdQLI = new Map<String,QuoteLineItem>();
            Map<Id,QuoteLineItem> mapExistingQLI;
            Map<Id,Quote_Line_Group__c> mapExistingQLG = new Map<Id,Quote_Line_Group__c>([SELECT Id,Name 
                                                                                            FROM Quote_Line_Group__c 
                                                                                            WHERE Quote__c = :cQuote.Id]);
            //Save the Quote
            upsert cQuote;
            //Remove the quotelineitems created that came from the existing opportunitylineitems
            mapExistingQLI = new Map<Id,QuoteLineItem>([SELECT Id,Quantity,UnitPrice,Setup_Fee__c,Data_Center__c,MRR__c 
                                            FROM QuoteLineItem 
                                            WHERE QuoteId =:cQuote.Id]);
            if(isNew && mapExistingQLI.values().size() > 0){
                delete mapExistingQLI.values();
            }
          /*  mapExistingQLI = new Map<Id,QuoteLineItem>([SELECT Id,Quantity,UnitPrice,Setup_Fee__c,Data_Center__c,MRR__c 
                                            FROM QuoteLineItem 
                                            WHERE QuoteId =:cQuote.Id]);
                                            */
            for(DataCenter__c dc : [SELECT Id, Name,Name_Abbr__c, kWCost__c, kW_Markup__c from DataCenter__c]){
                mapDCNameIds.put(dc.Name_Abbr__c,dc.Id);
            }
            //Upsert the new groupings
            Set<String> setQLGNames = new Set<String>();
            for(JQGridItem jqgi : lstJQGI){
                setQLGNames.add(jqgi.strGroupName);
                if(mapQLGs.containsKey(jqgi.strGroupName)){
                    mapQLGs.get(jqgi.strGroupName).Order__c = jqgi.decGroupOrder;
                }
            }
           List<Quote_Line_Group__c> lstQLG = new List<Quote_Line_Group__c>();
           //  Map<ID,Quote_Line_Group__c> lstQLG = new map<Id,Quote_LIne_Group__C>();
            for(Quote_Line_Group__c qlg : mapQLGs.values()){
                if(qlg.Quote__c != null){
                    lstQLG.add(qlg);
                   // lstQLG.put(qlg.Id,qlg);
                } else {
                    Quote_Line_Group__c qlgNew = new Quote_Line_Group__c(Quote__c=cQuote.Id,Name=qlg.Name,Order__c=qlg.Order__c);
                    lstQLG.add(qlgNew);
                   // lstQLG.put(qlg.ID,qlg);
                    //lstQLG.put(qlg.name,qlg);
                }
            }
            upsert lstQLG;
          // upsert lstQLG.values();
            //System.debug(lstQLG);
            //Delete the groupings that are no longer in use
            List<Quote_Line_Group__c> lstDeleteQLG = new List<Quote_Line_Group__c>();
            for(Quote_Line_Group__c qlg : mapExistingQLG.values()){
                if(mapQLGs.keySet().contains(qlg.Name) && !setQLGNames.contains(qlg.Name)){
                    lstDeleteQLG.add(qlg);
                }
            }
            delete lstDeleteQLG;
            //Resetup the map of quote line group name to id's
            mapQLGs.clear();
            mapExistingQLG = new Map<Id,Quote_Line_Group__c>([SELECT Id,Name,Quote__c,Order__c  
                                                                FROM Quote_Line_Group__c 
                                                                WHERE Quote__c = :cQuote.Id]);
            for(Quote_Line_Group__c qlg : mapExistingQLG.values()){
                mapQGNameIds.put(qlg.Name,qlg.Id);
                mapQLGs.put(qlg.Name,qlg);
            }
            //Upsert the new or existing QuoteLineItems
            for(JQGridItem jqgi : lstJQGI){
                QuoteLineItem qli;
                if((jqgi.strQLItemId.length() == 15 || jqgi.strQLItemId.length() == 18) && mapExistingQLI.containsKey(jqgi.strQLItemId)){ //15 or 18 is the valid size of an Id
                    qli = mapExistingQLI.get(jqgi.strQLItemId);
                } else {
                    qli = new QuoteLineItem(QuoteId=cQuote.Id,PricebookEntryId=jqgi.strPBEId);
                }
               // List<QuoteLineItem>  lstQLI1 = [SELECT Id,Pricebookentry.Product2.name FROM QuoteLineItem 
                 //       WHERE Id = :qli.Id  and pricebookentry.product2.isactive =false] ;
                
                map<Id, QuoteLineItem>  lstQLI1 = new map<id,QuoteLineitem>([SELECT Id,Pricebookentry.Product2.name FROM QuoteLineItem 
                        WHERE Id = :qli.Id  and pricebookentry.product2.isactive =false] );
                if (LstQli1.size()>0)                       
                
                {
                for(QuoteLineitem Qli1 :lstQLI1.values())
                {
             ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.ERROR,'Product '+Qli1.Pricebookentry.Product2.name + ' is Inactive');//Same old drama 
              ApexPages.addMessage(myMsg);
              return null;
             } 
             }
                qli.Quantity = jqgi.decQty;
                if(qli.Quantity != 0){
                   qli.UnitPrice = jqgi.decUSDMRR / qli.Quantity; 
                   system.debug('qli.UnitPrice ::'+qli.UnitPrice +'jqgi.decUSDMRR::'+ jqgi.decUSDMRR +'qli.Quantity::'+ qli.Quantity);
                } else {
                    qli.UnitPrice = 0;
                }
                
                qli.Discount__c = jqgi.decDiscount;
                //qli.Parent_Quote_Line__c = set this after everything has been saved as we need the id's that haven't been created yet
                if(mapDCNameIds.containsKey(jqgi.strDataCenter)){
                    qli.Data_Center__c = mapDCNameIds.get(jqgi.strDataCenter);
                }
                if(jqgi.strProdCompatId != null && jqgi.strProdCompatId != '') qli.Product_Compatibility__c = jqgi.strProdCompatId;
                qli.Product_Category__c = jqgi.strProdCat;
                qli.MRR__c = jqgi.decMRR;
                qli.Index__c = jqgi.decParentOrder;
                //system.debug('Saving QuoteLineItem '+jqgi.strQLIName+' with index '+jqgi.decParentOrder+' and MRR '+jqgi.decMRR);
                qli.Setup_Fee__c = jqgi.decSetupFee;
                qli.Formula_Exempt__c = Boolean.valueOf(jqgi.strExempt);
                qli.kW__c = jqgi.decKW;
                qli.CAPEX__c = jqgi.decCAPEX;
                qli.Cost__c = jqgi.decCost;
                qli.Markup__c = jqgi.decMarkup;
                qli.Base_Quantity__c = jqgi.baseQty;
                qli.Max_Discount__c = jqgi.maxDiscount;
                qli.Required__c = jqgi.strRequired;
                qli.Service_Type__c = jqgi.strServiceType;
                qli.Overage_Unit_Price__c = jqgi.decOveragePrice;
                qli.Unit_Of_Measure__c = jqgi.strUnit;
                if(mapQGNameIds.containsKey(jqgi.strGroupName)){
                    qli.Quote_Group__c = mapQGNameIds.get(jqgi.strGroupName);
                }
                qli.kw_Cost__c = jqgi.decKWCost;
                qli.kw_Markup__c = jqgi.decKWMarkup * 100;
                qli.DatapipeListPriceUSD__c = jqgi.decDPLPUSD;
                qli.Name__c = jqgi.strQLIName;
                system.debug('saving QLI '+qli.Name__c+' with Service Type '+qli.Service_Type__c);
                mapGridIdQLI.put(jqgi.strQLItemId,qli);
            }
            upsert mapGridIdQLI.values();
            //Add the parentid
            for(JQGridItem jqgi : lstJQGI){
                QuoteLineItem qli = mapGridIdQLI.get(jqgi.strQLItemId);
                if(jqgi.strParentId != null && jqgi.strParentId != '' && jqgi.strParentId != jqgi.strQLItemId && mapGridIdQLI.get(jqgi.strParentId)!=null){
                    qli.Parent_Quote_Line__c = mapGridIdQLI.get(jqgi.strParentId).Id;
                }
            }
            update mapGridIdQLI.values();
            //Delete the QuoteLineItems that are not in the grid
            List<QuoteLineItem> lstDeleteQLI = new List<QuoteLineItem>();
            for(QuoteLineItem qli : mapExistingQLI.values()){
                if(!mapGridIdQLI.containsKey(qli.Id)){
                    lstDeleteQLi.add(qli);
                }
            }
            delete lstDeleteQlI;
            //Update the existing group list with new groups
            cQuoteGroups = new List<SelectOption>();
            cQuoteGroups.add(new SelectOption('','--None--'));
          //  lstQLG = new map<Id,Quote_line_group__c>([SELECT Id,Name FROM Quote_Line_Group__c WHERE Quote__c = :cQuote.Id and name not in('Change','Remove') Order By Order__c ASC]);
           // for(Quote_Line_Group__c qlg : lstQLG.values()){
           
            lstQLG = [SELECT Id,Name FROM Quote_Line_Group__c WHERE Quote__c = :cQuote.Id Order By Order__c ASC];
            for(Quote_Line_Group__c qlg : lstQLG){
                if(qlg.Name != 'Change' && qlg.Name != 'Remove'){
                    cQuoteGroups.add(new SelectOption(qlg.Name,qlg.Name));
                    // cQuoteGroups.put(new SelectOption(qlg.Name,qlg.Name));
                }
            }
            //Sync/unsync the Quote to the Opp based on active checkbox
            if(cOpportunity.SyncedQuoteId == cQuote.Id && cbolActive == false){
                cOpportunity.SyncedQuoteId = null;
                update cOpportunity;
            } else if(cOpportunity.SyncedQuoteId != cQuote.Id && cbolActive){
                cOpportunity.SyncedQuoteId = cQuote.Id;
                update cOpportunity;
            } 
            //prReturn = new PageReference('/' + cQuote.Id);
            ApexPages.addMessage(new ApexPages.message(ApexPages.Severity.CONFIRM,cQuote.Name + ' saved'));
        } catch (Exception e){
            ApexPages.addMessage(new ApexPages.message(ApexPages.Severity.ERROR,e.getMessage()));
            Database.rollback(sp);
            //Clone the quote except the id as the Id may be kept even though the DB was rolled back
            cQuote = cQuote.clone(false,true,false,false);
        }
        return prReturn;
    }
    
apex sfdevapex sfdev
Future annotation provides enhances higher limits, try like this,
 
@future
public PageReference saveAll(){
  // your code
}

Find more details on @future annotation in https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_enhanced_future_overview.htm