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
Mamatha SrinivasMamatha Srinivas 

In following apex class Number of Queries is 27 why?


public class AdjustingSalesPrice {

    public static void UpdateSalesPrice(List<Opportunity> newList) {
        
         try{
            
            Set<Id> oppId = new Set <Id> ();
            
            for(Opportunity op : newList){
                oppId.add(op.Id);
                system.debug('Opportunity id:' +oppId);
            }
            Double Num;
             Double Num3;
             Double Num2;
             Double quantity = 0;
            List<Opportunity> OppList = [SELECT Id, Name, Target_R4WR__c,List_Recurring_4_Week_Rev__c, Upselling__c,Target_Recurring_Discount__c,
                                         (SELECT Id, Torq_One_Time__c, UnitPrice,ListPrice FROM OpportunityLineItems where Torq_One_Time__c = false )
                                         FROM Opportunity WHERE Id=:oppId limit 1];
             
              List<OpportunityLineItem> OliList =  [SELECT id, OpportunityId, UnitPrice, Torq_One_Time__c,Quantity
                                                FROM OpportunityLineItem
                                                WHERE OpportunityId IN :oppId
                                                AND Torq_One_Time__c = False AND UnitPrice > 0];
             
             system.debug('ListSizeof Oppline' +OliList.size());
             for(OpportunityLineItem Oli:OliList){
               Quantity = quantity+Oli.Quantity;
                 Num3 = Quantity;
             }
            system.debug('ListSize of Num3' +Num3);
         
            List<OpportunityLineItem> updateOppOli = new List <OpportunityLineItem>();
            List<Opportunity> updateOpp = new List <Opportunity>();
            
            for(Opportunity opp: OppList){
                
                for(OpportunityLineItem Oli: opp.OpportunityLineItems){
                    
                    System.debug('OpportunityLineItems:' +oli);
                    if(Opp.Upselling__c == True){
                    if(Oli.Torq_One_Time__c == false &&  opp.Target_R4WR__c > opp.List_Recurring_4_Week_Rev__c && Oli.UnitPrice > 0 && Oli.UnitPrice == Oli.ListPrice){
                     Num = opp.Target_R4WR__c - opp.List_Recurring_4_Week_Rev__c; 
                        system.debug('Minus value'+Num);
                     Num2 = Num/Num3;
                        system.debug('divided value'+Num2);
                        Oli.UnitPrice = Num2 + Oli.UnitPrice;
                        updateOppOli.add(Oli);
                      System.debug('UpdateSalesPrice Number of Queries used in this apex code so far: ' + Limits.getQueries()); 
                    }
                    }
                    
                    if(opp.Upselling__c == false){ 
                        if(Oli.Torq_One_Time__c == false &&  opp.Target_R4WR__c == Null && opp.Target_Recurring_Discount__c == Null && Oli.UnitPrice > 0 ){
                        Oli.UnitPrice = Oli.ListPrice;
                        system.debug('Unit Price'+oli.UnitPrice);
                        updateOppOli.add(Oli);
                    
                   system.debug('updateOppOli'+updateOppOli);
                    }
                    }
    }
      }
           
               Map<Id, OpportunityLineItem> Olimap = new Map<Id, OpportunityLineItem>();
            Olimap.putAll(updateOppOli);
           
            if(Olimap.size() > 0){
                update Olimap.values();
                system.debug(Olimap.size()+' Opportunity Line Items Updated sucessfully: '+Olimap.values());
            }
         }
        catch(Exception e){
            System.debug('Exception Occured::'+e.getMessage());
        }
}
}
MKRMKR
Hi,

The number of queries also depend on the other DML statements that this class triggers. Most probably. the following statement runs some trigger logic / process automation which runs queries:
 
update Olimap.values();

The class itself is well bulkified and does not seem to run unnecessary queries.

Regards,
MKR
Mamatha SrinivasMamatha Srinivas
Hi MKR ,

Thank you so much for giving your valuable time, I want know how we can avoid this queries.
MKRMKR
Hi,

To help you with query avoidance, it would be helpful to see the triggers run on opportunity line items. There it might be possible to avoid certain queries, but it is good to know that the those queries are there for a reason to execute certain business logic everytime opportunity line items are saved.

Regards,
MKR