I created this aggregateResult[] that returns the data I want, as you can see below i store the values in a List, everything works as I want but now I am completely stuck in the test class. I have to use List1.Size() to do some validations and I have many IF Statements and basically a test method for each 'IF'-'ELSE IF' condition. everything was doing fine until I get to the List1.size()==14 I am getting the error 'too many queries'. So this approach will not work. I was wondering if there is another way to test these 'if statements' or to set the List.size() straight forward in the test class. I will appreciate any help!

    //for mws chart
  AggregateResult[] groupedss 
  =[SELECT CALENDAR_MONTH(Purchase_Date__c),CALENDAR_YEAR(Purchase_Date__c), SUM(Detail_Total__c)
   FROM MWSOrderItem__c 
   WHERE  Purchase_Date__c = LAST_N_DAYS:365 AND Buyer_lookup__c =:UserInfo.getUserId()
   GROUP BY CALENDAR_MONTH(Purchase_Date__c),CALENDAR_YEAR(Purchase_Date__c)
   ORDER BY CALENDAR_YEAR(Purchase_Date__c),CALENDAR_MONTH(Purchase_Date__c) ASC ];

 List1 = new List<String>();  

 for (AggregateResult ar11 : groupedss )  {



        //do something

   }else if(list1.size()==2){
        //do something
   }else if(list1.size()==4){
   }else if(list1.size()==24){
        //do something

test class

    public class test_dashBoardPOIssues {

  static testMethod void testMoveOpenOnly2() {

    Profile pf = [Select Id from Profile where Name = 'System Administrator'];
    User   u2 = new User();
    u2.FirstName = 'Test';
    u2.LastName = 'User';
    u2.Email = 'testuser@test123456789.com';
    u2.CompanyName = 'test.com';
    u2.Title = 'Test User';
    u2.Username = 'testuser@test123456789.com';
    u2.Alias = 'testuser';
    u2.CommunityNickname = 'Test User';
    u2.TimeZoneSidKey = 'America/Mexico_City';
    u2.LocaleSidKey = 'en_US';
    u2.EmailEncodingKey = 'ISO-8859-1';
    u2.ProfileId = pf.Id;
    u2.LanguageLocaleKey = 'en_US';
   insert u2;

    Account account2 = new Account(Name = 'Test Account1', Buyer__c='carlos', Buyer_Email__c='carlos@netrush.com');
    insert account2;

     NRProducts__c product2 = new NRProducts__c(Account__c = account2.Id,Buyer_Lookup__c=u2.id, Name = '105823437');
   insert product2;

     MWSOrder__c mws2 = new MWSOrder__c (Name='12',AmazonOrderId__c='12', PurchaseDate__c=datetime.newInstance(2014, 5, 3) );
    insert mws2;

     MWSOrderItem__c mwsItem2 = new MWSOrderItem__c (MWSOrder__c= mws2.id,  OrderItemId__c='2',NRProduct__c=product2.id,Buyer_Lookup__c=u2.id );      
    insert mwsItem2;

     MWSOrderItem__c mwsItem3 = new MWSOrderItem__c (MWSOrder__c= mws2.id,  OrderItemId__c='3',NRProduct__c=product2.id,Buyer_Lookup__c=u2.id );      
    insert mwsItem3;         


  dashBoardPOIssues sc2 = new dashBoardPOIssues (new ApexPages.StandardController(u2)); 


   System.assertEquals(2, sc2.List2.Size()); 


 //list size 4
   static testMethod void testMoveOpenOnly4() {

    Profile pf = [Select Id from Profile where Name = 'System Administrator'];
    User   u4 = new User();
    u4.FirstName = 'Test';
    u4.LastName = 'User';
    u4.Email = 'testuser@test123456789.com';
    u4.CompanyName = 'test.com';
    u4.Title = 'Test User';
    u4.Username = 'testuser@test123456789.com';
    u4.Alias = 'testuser';
    u4.CommunityNickname = 'Test User';
    u4.TimeZoneSidKey = 'America/Mexico_City';
    u4.LocaleSidKey = 'en_US';
    u4.EmailEncodingKey = 'ISO-8859-1';
    u4.ProfileId = pf.Id;
    u4.LanguageLocaleKey = 'en_US';
   insert u4;

    Account account4 = new Account(Name = 'Test Account1', Buyer__c='carlos', Buyer_Email__c='carlos@netrush.com');
    insert account4;

     NRProducts__c product4 = new NRProducts__c(Account__c = account4.Id,Buyer_Lookup__c=u4.id, Name = '105823437');
   insert product4;
     MWSOrder__c mws4 = new MWSOrder__c (Name='1234',AmazonOrderId__c='1234', PurchaseDate__c=datetime.newInstance(2014, 4, 3) );
    insert mws4;

      MWSOrder__c mws5 = new MWSOrder__c (Name='12345',AmazonOrderId__c='12345', PurchaseDate__c=datetime.newInstance(2014, 3, 3) );
    insert mws5;

      MWSOrderItem__c mwsItem41 = new MWSOrderItem__c (MWSOrder__c= mws4.id,  OrderItemId__c='41',NRProduct__c=product4.id,Buyer_Lookup__c=u4.id );      
    insert mwsItem41;

     MWSOrderItem__c mwsItem42 = new MWSOrderItem__c (MWSOrder__c= mws5.id,  OrderItemId__c='42',NRProduct__c=product4.id,Buyer_Lookup__c=u4.id );      
    insert mwsItem42;


  dashBoardPOIssues sc4 = new dashBoardPOIssues (new ApexPages.StandardController(u4)); 


  System.assertEquals(4, sc4.List2.Size()); 

  • It usually means you are doing a query somewhere inside of a loop. You need to fix this and you won't get that error anymore. Your issue is likely in a trigger.
    – dphil
    Jun 10 '14 at 17:39
  • dphil, i already checked the code an all the inserts are outside the loops. So this testing approach is correct? I am very new in Salesforce and I just want to make sure I am in the right track. thank you!
    – Carlos
    Jun 10 '14 at 17:48
  • I'm not talking about DML statements. I'm talking about queries. Both will cause issues if they are in loops.
    – dphil
    Jun 10 '14 at 17:49
  • I will double check that
    – Carlos
    Jun 10 '14 at 17:57

There are many ways to address your issue:

1) you can move all code in your if statements and in various functions i.e. fun1,fun2...24

and call these function from your test class.

2) you can use static variable in your if condition and then control flow using that variable.


if(list1.size()==0 OR Stat_var == 0){
    Fun1() //call function 1 
}else if(list1.size()==2 OR Stat_var == 0){
    Fun2() //call function 1

I suggest you to read as many sample code as possible.


  • AtulRajguru9, could you please explain me the option 1, i think that can solve this issue but i don't know how to do that, i am very new in Apex. i will appreciate your hep. Thank you in advance!
    – Carlos
    Jun 11 '14 at 18:35
  • 1
    I have updated my answer. So basically you will need to move all code from if block to a function fun1 (and so on)and then call fun1 from your test class. Jun 12 '14 at 4:45
  • BTW... why the negative vote for answer? Jun 12 '14 at 5:02
  • I did not vote on any answer my friend.. i don't know why you see that.
    – Carlos
    Jun 12 '14 at 17:17
  • AtuRajguru9, could you please share a link where a can see an example for this.. i don't quite understand this logic due to my entry level experience in Salesforce. i will appreciate it. Thank you in advance!
    – Carlos
    Jun 12 '14 at 18:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.