+ Start a Discussion
tonante27tonante27 

SOQL Governor Limits Exception Error When using Apex Controller

HI,

I have to write code that takes a list of  of International CLubs and calculates a forecast registration count per each member of that club for a grand total.  My problem is that I am facing governor limits on a simple query but it does return  roughly 4K records before being fed into an Apex Controller to display 300 records per page.  What can I do to avoid hitting these limits?  Do I need to Bulkify more?  How?  Here is the code and thanks much for your help (I have highlighted  the SOQL statement in Bold ):

public class AccountsCTRL_New
{
    String CountryName = '';
    Boolean NextClick = false;
    Boolean PrevClick = false;    
   // public List<Account> ClubberAccounts = new List<Account>();
 
    public PageReference churchInfo()
    {      
        try
        {
        NextClick = false;
        prevClick = false;
        CountryName  = Apexpages.currentPage().getParameters().get('name');
  
                                                                                                                              
        }
        catch(Exception e)
        {
            ApexPages.addMessages(e);
        }             
        return null;
    }
    
    
public ApexPages.StandardSetController setCon
     {
        get
        {   
           if(setCon == null)
           {  
           List<account> accountList = new List<Account>();
               setCon  = new ApexPages.StandardSetController(accountList);                                                           
           }
           else
           {                  
            if(CountryName != '' )  //BELOW IS THE PROBLEM SOQL STATEMENT
            {  
                if( NextClick == false && PrevClick == false)                
                setCon = new ApexPages.StandardSetController(Database.getQueryLocator(
                                                  [SELECT Id,
                                                          Name,
                                                          Territory__c,
                                                          Mailing_City__c,
                                                          ClubberCount__c,
                                                          Mailing_Address_Book__c,
                                                          ccountTest__c,
                                                          Type
                                                   FROM Account
                                                   WHERE Territory__c =: CountryName
                                                   AND Customer_Type__c =: 'International Church'  
                                                   ]));
                                                   
             }                                                                                                                                                                                            
                 setCon.setPageSize(300);                                                                         
           }
           return setCon;
        }
        set;
    }
    
    public List<Account> getAccounts()
    {
         return (List<Account>) setCon.getRecords();        
    }
    
    public PageReference PrevList()
    {       
     if(setCon.getHasPrevious()){
         setCon.previous();
        PrevClick = true;   
        }                                        
        return null;
    }
    
    public PageReference NextList()
    {                  
      if(setCon.getHasNext()){
            setCon.next();
      NextClick = true;
          }
       return null;
    }             

        

}

 

 

Best Answer chosen by Admin (Salesforce Developers) 
tonante27tonante27

The problem to reduce governor limits was solved by removing the Apex Controller property thatincluded the query and placing the query within a new Page Reference method called  "Search". The controller object is just an empty  property:

So the solution is:

 

public class accountdisplylink 
{
        public string err{get;set;}
        public String ContryName{get;set;}    
        public ApexPages.StandardSetController con{get; set;}
          public List<Account> searchResults
            {
                get
                {
                    if(con != null)
                        return (List<Account>)con.getRecords();
                    else
                        return null ;
                }
                set;
            }               
        public pagereference search()
            {     
                ContryName = Apexpages.currentPage().getParameters().get('name');
                if(ContryName != null &&  !ContryName.trim().equals('') )
                {
              
               err='';
                 string baseQuery = 'Select Id,Name,Territory__c,Mailing_City__c,Mailing_Address_Book__c,ccountTest__c,Type from Account ';
                 string finalQuery = baseQuery +'WHERE Territory__c =\''+ContryName+'\' AND Customer_Type__c = \'International Church\' Order By Name ';
                 con = new ApexPages.StandardSetController(Database.getQueryLocator(finalQuery));
                 con.setPageSize(300);
                 }
         return null;
               }
           
           public void PREVIOUS() 
            {
                 con.PREVIOUS();            
            }
            public void NEXT() 
            {
                 con.next();
            }
            public Boolean hasNext
            {
            get
            {
            return con.getHasNext();
            }
            set;
            }           
            public Boolean hasPrevious
            {
            get
            {
            return con.getHasPrevious();
            }
            set;
            }
   
}