+ Start a Discussion
etechcareersetechcareers 

Number of SOQL queries: 95 out of 100????

Hi I keep getting these warnings:

Caused the following Apex resource warnings:

Number of SOQL queries: 95 out of 100

 

My Class is simple as this:How can I avoid this warning or write it any other way?Thanks

Z

 

public class compensationreport{
    public string AAid{get;set;}
    public string AAWid{get;set;}
    public string AATid{get;set;}
    public string AACSid{get;set;}
    public string CompName{get;set;}
    public Integer Gross{get;set;}
    public Integer Withdrawn{get;set;}
    public Integer Transferred{get;set;}
    public Integer Customers{get;set;}
    public String Level { get { return Level ;} set { Level=value ; } } 

    public List<GrossHolder> queryResults{ get; set; }  
    public class GrossHolder {       
        public String AAName {get; set;}
        public Integer TotalGross {get; set;}
        public String compid{get;set;}    
        public GrossHolder(){}
    }
    
/*******************************************TOTAL Withdrawn******************************************************************************************/

    public List<WithdrawnHolder> queryResultsW{ get; set; }  
    public class WithdrawnHolder {
        public Integer TotalW {get; set;}
        public String AANameW {get; set;}
        public String aax {get;set;}
        public WithdrawnHolder(){}
    }
 
    public String getWithdrawn(String id){


                            
    AggregateResult[] groupedResults = [SELECT Ownerid,Count(Id) ce 
                            FROM Opportunity Where 
                            Withdrawn__c = True 
                            AND StageName = 'Closed Won' 
                            AND Withdrawal_Status__c ='Withdrawn' 
                            AND Withdrawn_Date__c = LAST_MONTH
                            AND Ownerid=:id 
                            AND (NOT Name Like '%COMP%')
                            GROUP BY Ownerid]; 
        
    queryResultsW = new List<WithdrawnHolder>();
    
    for (AggregateResult ar : groupedResults)  {
        WithdrawnHolder myObject = new WithdrawnHolder();
            myObject.aax = String.valueOf(ar.get('Ownerid'));
            AAWid =  myObject.aax;
            myObject.TotalW = (Integer) ar.get('ce');
        Withdrawn = myObject.TotalW ;    
        queryResultsW.add(myObject); 
    }
        return null;    
}  
/*******************************************TOTAL TRANSFERRED******************************************************************************************/
    public List<WithdrawnHolderT> queryResultsWT{ get; set; }  

    public class WithdrawnHolderT {

        public Integer TotalWT {get; set;}
         public String AANameWT {get; set;}
         public String aaxT {get;set;}
    public WithdrawnHolderT(){
    }
    }
 
    public String getWithdrawnT(String id){

             
    AggregateResult[] groupedResults = [SELECT Ownerid,Count(Id) ce 
                            FROM Opportunity Where 
                            Withdrawn__c = True 
                            AND StageName = 'Closed Won' 
                            AND Withdrawal_Status__c = 'Transferred to DL' 
                            AND Withdrawn_Date__c = LAST_MONTH  
                            AND Ownerid=:id 
                            AND (NOT Name Like '%COMP%')
                            GROUP BY Ownerid];         
        
    queryResultsWT = new List<WithdrawnHolderT>();
    
    for (AggregateResult ar : groupedResults)  {
        WithdrawnHolderT myObject = new WithdrawnHolderT();
        myObject.aaxT = String.valueOf(ar.get('Ownerid'));
        AATid =  myObject.aaxT;
        myObject.TotalWT = (Integer) ar.get('ce');
        Transferred = myObject.TotalWT ;
    
    queryResultsWT.add(myObject); 
      
    }
    return null;    
    }  
/*******************************************TOTAL CUSTOMERS******************************************************************************************/    
    public List<customers> queryResultscs{ get; set; }  
    public class customers {

        public Integer TotalCS {get; set;}
         public String AANameCS {get; set;}
         public String aaxCS {get;set;}
    public customers(){
    }
    }
 
    public String getCustomers(String id){


    
    AggregateResult[] groupedResults = [SELECT Ownerid,Count(Id) ce 
                                        FROM Account 
                                        Where Owner.Level__c = 'AA' 
                                        AND Ownerid=:id AND CreatedDate = LAST_MONTH
                                        GROUP BY Ownerid]; 
                    
    queryResultscs = new List<customers>();
    
    for (AggregateResult ar : groupedResults)  {
        customers myObject = new customers();
        myObject.aaxCS = String.valueOf(ar.get('Ownerid'));
        AACSid =  myObject.aaxCS;
        myObject.TotalCS = (Integer) ar.get('ce');
        Customers = myObject.TotalCS ;
    
    queryResultscs.add(myObject); 
      
    }
    return null;    
    }                               
/*******************************************Actual******************************************************************************************/     
public PageReference query() { 
Date Lastmonthfirst;
Date Lastmonthend;

Date selectedDate =  Date.Today(); //Give your date
Date firstDate = selectedDate.toStartOfMonth();
Date lastDate = firstDate.addDays(date.daysInMonth(selectedDate.year() , selectedDate.month())  - 1);
system.debug(firstDate  + ' **** ' +lastDate );
//******************************************************************************************************
Lastmonthfirst = selectedDate.toStartOfMonth().addMonths(-1);
Lastmonthend = Lastmonthfirst.addDays(date.daysInMonth(Lastmonthfirst.year() , Lastmonthfirst.month())  - 1);
system.debug(Lastmonthfirst  + ' **** ' +Lastmonthend );

     
    List<Compensation__c>  delmarker = new List<Compensation__c>(); 

    delmarker=[select id from Compensation__c where Date_ran__c =LAST_MONTH];

    
    for(Compensation__c sm:delmarker){
        delete sm;
    }  
       
       
        
  If (Level=='AA'){
       
              AggregateResult[] groupedResults = [SELECT Ownerid, Count(Id) ce
                                                 FROM Opportunity Where Comp__c=False  
                                                 AND CloseDate=LAST_MONTH 
                                                 AND StageName = 'Closed Won'
                                                 GROUP BY Ownerid];  

                                      
    queryResults = new List<GrossHolder>();
    for (AggregateResult ar : groupedResults)  {
        GrossHolder myObject = new GrossHolder();
            myObject.TotalGross = (Integer) ar.get('ce');
            myObject.compid = String.valueOf(ar.get('Ownerid'));
            AAid = myObject.compid;
            
            getWithdrawn(AAid);
            getWithdrawnT(AAid);
            getCustomers(AAid);
/*****************************INSERT COMP**********************************************************************/

            Compensation__c cmp = new Compensation__c();
            cmp.Name__c = myobject.compid;
            cmp.Comp_Plan__c ='AA';
            cmp.Total_Gross__c = myObject.TotalGross;
            cmp.Date_ran__c = Lastmonthfirst;
                
               if(AAWid!=AAid){
                    cmp.Total_Withdrawn__c = 0;
                }
                else{
                    cmp.Total_Withdrawn__c = Withdrawn;
                    if(cmp.Total_Withdrawn__c == null){
                        cmp.Total_Withdrawn__c = 0;
                    }                    
                }
                 
                if(AATid!=AAid){
                    cmp.Total_Transfers__c = 0;
                }
                else{
                    cmp.Total_Transfers__c = Transferred;
                    if(cmp.Total_Transfers__c == null){
                        cmp.Total_Transfers__c = 0;
                    }
               } 
               
                if(AACSid!=AAid){
                    cmp.Total_Customers__c = 0;
                }
                else{
                    cmp.Total_Customers__c = Customers;
                    if(cmp.Total_Customers__c == null){
                        cmp.Total_Customers__c = 0;
                    }
               }                                
                insert cmp;
        queryResults.add(myObject); 
        }
        }
        return null;
    } 
}

 

Best Answer chosen by Admin (Salesforce Developers) 
kiranmutturukiranmutturu

that depeneds on the how the process was initiated .....and the number of classes and triggers are there in the run....you need to take the complete invocation process and analyse what are all the other apex classes or triggers are calling in the same invocation.