You need to sign in to do that
Don't have an account?
etechcareers
Too many SOQL queries: 101???
Hi Everyone:
I do not understand why is my getCustomers method timing out to too many SOQL Queries?? When I remove the method it works...
Here is the error on my log:
Class.compensationreport.getCustomers: line 148, column 1
Class.compensationreport.query: line 227, column 1
Please help....and I keep getting Apex Governor Limit Warning that states Number of SOQL queries: 77 out of 100???
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 String Monthrun { get { return Monthrun ;} set { Monthrun=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){ List<SObject> qrystringW = new List<SObject>(); if(Monthrun=='THIS_MONTH'){ qrystringW = [SELECT Ownerid,Count(Id) ce FROM Opportunity Where Withdrawn__c = True AND StageName = 'Closed Won' AND Withdrawal_Status__c ='Withdrawn' AND Withdrawn_Date__c = THIS_MONTH AND Ownerid=:id AND (NOT Name Like '%COMP%') GROUP BY Ownerid]; } else { qrystringW = [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]; } AggregateResult[] groupedResults = qrystringW; 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){ List<SObject> qrystringT = new List<SObject>(); if(Monthrun=='THIS_MONTH'){ qrystringT = [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 = THIS_MONTH AND Ownerid=:id AND (NOT Name Like '%COMP%') GROUP BY Ownerid]; } else { qrystringT = [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]; } AggregateResult[] groupedResults = qrystringT; 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){ List<SObject> qrystringc = new List<SObject>(); if(Monthrun=='THIS_MONTH'){ qrystringc = [SELECT Ownerid,Count(Id) ce FROM Account Where Owner.Role_Name__c Like '%Advisor%' AND Ownerid=:id AND CreatedDate = THIS_MONTH GROUP BY Ownerid]; } else{ qrystringc = [SELECT Ownerid,Count(Id) ce FROM Account Where Owner.Role_Name__c Like '%Advisor%' AND Ownerid=:id AND CreatedDate = LAST_MONTH GROUP BY Ownerid]; } AggregateResult[] groupedResults = qrystringc; 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>(); If(Monthrun=='THIS_MONTH'){ delmarker=[select id from Compensation__c where Date_ran__c =THIS_MONTH]; } else{delmarker=[select id from Compensation__c where Date_ran__c =LAST_MONTH];} for(Compensation__c sm:delmarker){ delete sm; } List<SObject> qrystringf =new List<SObject>(); if(Monthrun=='THIS_MONTH'){ qrystringf=[SELECT Ownerid, Count(Id) ce FROM Opportunity Where Comp__c=False AND CloseDate=THIS_MONTH AND StageName = 'Closed Won' GROUP BY Ownerid]; } else { qrystringf=[SELECT Ownerid, Count(Id) ce FROM Opportunity Where Comp__c=False AND CloseDate=LAST_MONTH AND StageName = 'Closed Won' GROUP BY Ownerid]; } If (Level=='AA'){ AggregateResult[] groupedResults = qrystringf; 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; IF(Monthrun=='THIS_MONTH'){ cmp.Date_ran__c = date.TODAY(); } else{ 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; } }
In your queries, when you use variables like this
Use : before the variables.
Try changing the queires.
Hi:
Thanks again for helping out, but where are you declaring that variable? I get the error:
Error: Compile Error: Variable does not exist: THIS_MONTH at line 40 column 54
Thanks
Z
then why are you using it in the above statements?
Is withdrawn_Date__c, a date field or a text Field?
It is a Date Field...
So here is the thing, I have a picklist value on my VF page Like this:
Monthrun in Apex code I had to place as string, I tried it as date but it did not work...
So if they select this month it would run all data this month ... that is why you see the if then statement there...
The variable is a string 'THIS_MONTH' which Monthrun holds the value...
Once they run this compensation plan, it would along this run the trigger you helped me out with in the previous help...
But the trigger is running but it does not run with the getCustomers method....If I deactivate the trigger then this class works... If I make the trigger active then getcustomers do not work...
I tried to change the trigger previously to do it after insert,after update but I got an error stating
Apex trigger bonusAmount caused an unexpected exception, contact your administrator: bonusAmount: execution of AfterUpdate caused by: System.FinalException: Record is read-only: Trigger.bonusAmount: line 45, column 1
you see my dilema....
You might need to change the above line. you are comparing date field with string.
Hi thank you for everything but do you know how i can capture aggregate results in my test class... this is what i got so far which is up to 47%...for the above Apex class within this discussion.
thanks
z
Happy holidays
if possible can you post the class, it would be much easier to write a test class.
I got 58% but query is not being pick up correctly?
thanks
Z
I got it to 71%...
but nothing in query() method is being called... Would you know why?
Thanks
Happy Holidays