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
MSRMSR 

Hitting View State Error on a Standard controller page with Pagination and Sorting

HI All,

 

I am hitting a View State error for the controller that i wrote for a Vf page in which an input text is entered to retrive the results.In the controller it queries the list from a custom object and populates on a page. I used std controller for pagination to run through the results to show 10000 records atleast out of 40000 records records.My Issue is when i keep the pagesize in the std controller to 100,if i click on "Next/Previous "  button tto run through results of Say 2000, i am hitting the view state error after 1300-1400 records.Can someone debug my controller code and help me where i am going wrong.I have tried to declare transients and tried to follow all the norms to minimize view state error but still i am seeing the error.Any suggestions....appriciated.Below is my controller code.

public class TestIpreoSearch {

   public transient List<Ipreo__c> ipresultList {get;set;}
   public transient List<Trading_Activity__c> selectedTAs {get;set;}
   public List<Trading_Inquiry__c> selectedTIs {get;set;}
   public transient List<cTradingActivity> TAList {get;set;}
   public transient List<cTradingInquiry> TIList {get;set;}
   public String inputValue { get; set; }
   //public String radiooption{ get; set; }
   public integer totalSize{get;set;}
   public integer totalSize1{get;set;}
   public boolean showcolumn {get;set;}
   public boolean Mycoverage {get;set;}
   public boolean donotshow {get;set;} // This boolean value is to hide the TI Section when Desk manager selects 'MyDesk' RadioOption;
   public boolean SingleUserCoverage {get;set;}
   public boolean Mydeskcoverage {get;set;}
   public boolean ManagerVisiblity{get;set;}
   public Set<String> AccTeamMemidSet = new Set<String>();
 // public List<SelectOption> options{get;set;}
   public String sortDirection = 'DESC';
   public String sortDirectionTi = 'DESC';
   public String sortExpr = 'Trade_Date__c';
   public String sortExprTi = 'Date__c ';
   public Boolean sortFlag = false;
   public Boolean sortFlagTi = false;
   
      public TestIpreoSearch (){
          string roleid = userinfo.getUserRoleId(); 
          system.debug('ROLE ID:::'+roleid);
           
           if((roleid == '00EU0000000nAblMAE') || (roleid =='00EU0000000nAboMAE')) {
                showcolumn = true; 
               }else
                showcolumn = false; 
                system.debug('ENTER LOOP:::'+showcolumn);
                
         /*  options = new List<SelectOption>(); 
           options.add(new SelectOption('MyCoverage','My')); 
           options.add(new SelectOption('MyDeskCoverage','My Desk')); */   
       // getTALists();         
      
     }
      
        public List<SelectOption> getRadioSelection() {
        List<SelectOption> RadioSelection= new List<SelectOption>(); 
        RadioSelection.add(new SelectOption('MyCoverage','My')); 
        RadioSelection.add(new SelectOption('MyDeskCoverage','My Desk')); 
         return RadioSelection;
      } 
      String Radiooption = null;
      public String getRadiooption() {
        return Radiooption ;
    }
      public void setRadiooption(String Radiooption)
    {
        this.Radiooption = Radiooption;
    }
       
    
     
     
   public ApexPages.StandardSetController con { 
     get{
       if(con == null || sortFlag == true){
       String sortFullExp = sortExpression  + ' ' + sortDirection;
       String message = System.currentPagereference().getParameters().get('msg');
       String sortmessage = System.currentPagereference().getParameters().get('msg2');
       system.debug('CON CALLED:::::');
       string Userid = userinfo.getUserId();
       String pfid = userinfo.getprofileid();
       //List<AccountTeamMember> AccTeamMemList = new List<AccountTeamMember>();
       
       Set<ID> uidset = new Set<ID>(); 
             
       if(showcolumn == false){
              SingleUserCoverage = true;
          }
        if( SingleUserCoverage == true){
          uidset.add(UserId);
          system.debug('SINGLEUSERSET'+uidset);
          donotshow = true;
       }
          
        
       if(radiooption == 'MyCoverage'){
          uidset.add(UserId);
          system.debug('USERSET'+uidset);
          donotshow = true;
       }else if(radiooption == 'MyDeskCoverage'){
          donotshow = false;
         // List<User> uList = [SELECT id,name,profileid FROM user WHERE profileid=:pfid];
          for(User u:[SELECT id,name,profileid FROM user WHERE profileid=:pfid]){
            uidset.add(u.id);
            system.debug('DESKSET'+uidset);
          }  
        }
      
       // AccTeamMemList = [SELECT accountId, userId FROM AccountTeamMember WHERE userId IN: uidset]; 
         for(AccountTeamMember a:[SELECT accountId, userId FROM AccountTeamMember WHERE userId IN: uidset]){
                 AccTeamMemidSet.add(a.accountId);
         } 
         
         String SearchQuery ='';
        List<String> slist = new List<String>();
           String s='';
       if(AccTeamMemidSet.size() > 0){    
           slist.addAll(AccTeamMemidSet);
                
          for(integer i=0;i<slist.size();i++){
            if(i!=0)
            s+='OR Account__c='+'\''+slist[i]+'\'';
            } 

        s+=')';
      }   
           string whereclause = 'WHERE';
           whereClause += ' (CUSIP__c ='+'\''+inputValue+'\'';  
           whereClause += ' OR Bloomberg_Issuer_Ticker__c ='+'\''+inputValue+'\'';
           whereClause+=')';
           if(s != '')
             whereClause += ' AND ( Account__c =\''+slist[0]+'\''+s+'';
             whereClause += ' ORDER BY '+sortFullExp+' LIMIT 1000';
               
           searchQuery = 'SELECT id,name, Trade_ID__c, Trade_Date__c, Bloomberg_Issuer_Ticker__c, Sales_Person__c, '+
                         'Account__r.Name, CUSIP__c, Coupon_Rate__c, Maturity_Date__c, Buy_Sell__c, '+
                         'Par_Notional__c, Trade_Price__c, Spread__c, Sales_Credit__c '+
                         'FROM Trading_Activity__c '+whereClause;    
     
                      
          system.debug('&&&&&&&&QUERY:::::'+searchQuery);
          
          
                    
          con = new ApexPages.StandardSetController(Database.getQueryLocator(searchQuery));
               if(message == 'success' || sortmessage == 'aftersort')
                con.setPageSize(100);
                else
                 con.setPageSize(10);
                totalSize= con.getResultSize(); 
                sortFlag = false;
}
Return con;
}
Set;
}

/*-----------------------------SEARCH METHOD FOR USER INPUT-------------------------------------------*/
   
     
     public void SearchData() {
      
      con = null;
     // TAList = null;
    //  con1 = null;
     // TIList = null;
        
    /*  ipresultList =  [SELECT Account_Name__c,CUSIP__c,Ticker__c,Portfolio_Name__c,
                                 Par_Held__c,Par_Change__c,Position_Date__c,Source__c,
                                 Coupon_Rate__c,Maturity_Date__c 
                           FROM Ipreo__c
                          WHERE (CUSIP__c=:inputvalue OR Ticker__c=:inputvalue) ];*/
             
         
   }      
        
 /*----------------------------------END OF SEARCH METHOD--------------------------------------------*/ 
    
        
    //This method uses a simple SOQL query to return a List of TradingActivities
    public List<cTradingActivity> getTALists() {
            //  if(TAList == null) {
              TAList = new List<cTradingActivity>();
            for(Trading_Activity__c ta:(List<Trading_Activity__c>)con.getRecords()) {
                // As each TradingActivity is processed we create a new cTradingActivity object and add it to the TAList
                TAList.add(new cTradingActivity(ta));
              }
          // }
        return TAList;
    }
    
 public pagereference gotolist(){
        con = null;TAList = null;
        PageReference pageRef = Page.gotoTAlistIpreo1;
        pageRef.getParameters().put('msg','success');
       // pageRef.setRedirect(true);
        return PageRef;
      }
      
 public pagereference gototilist(){
        con1 = null;TIList = null;
        PageReference pageRef = Page.gotoTIlistIpreo;
        pageRef.getParameters().put('msg1','success');
       // pageRef.setRedirect(true);
        return PageRef;
      }     
      
  public pagereference ipgotolist(){
         pagereference p2 = new pagereference('/apex/gotoipList');
         return p2;
      }   
        
/* ----------------------WRAPPER FOR TRADING ACTIVITIES--------------------------------------------------*/
     
      
    public PageReference processSelected() {

                //We create a new list of TAs that we be populated only with TAs if they are selected
        selectedTAs = new List<Trading_Activity__c>();

        //We will cycle through our list of cTradingActivities and will check to see if the selected property is set to true, if it is we add the TradingActivity to the selectedTAs list
        for(cTradingActivity cta : TAList) {
        
            if(cta.selected == true) {
                selectedTAs.add(cta.tra);
                System.debug('These are the selected TAs...'+selectedTAs);
            }
        }
        //TAList.clear();  //
        // Now we have our list of selected TradingActivities and can perform any type of logic we want, sending emails, updating a field on the TA, etc
        System.debug('These are the selected TAs...'+selectedTAs.size());
       /* for(Trading_Activity__c tra : selectedTAs) {
            system.debug(tra);
            system.debug('@@@'+tra);
        }*/
      return Page.gotoTAlistIpreoExcel;
    }


    // This is our wrapper/container class. A container class is a class, a data structure, or an abstract data type whose instances are collections of other objects. In this example a wrapper class contains both the standard salesforce object Contact and a Boolean value
    public class cTradingActivity {
        public Trading_Activity__c tra {get; set;}
        public Boolean selected {get; set;}

        //This is the contructor method. When we create a new cTradingActivity object we pass a TA that is set to the ta property. We also set the selected value to false
        public cTradingActivity(Trading_Activity__c ta) {
            tra = ta;
           selected = false;
        }
    }
    
    //This method uses a simple SOQL query to return a List of TradingActivities
   /* public List<cTradingActivity> getTALists() {
       //if(TAList == null) {
            TAList = new List<cTradingActivity>();
            for(Trading_Activity__c ta:(List<Trading_Activity__c>)con.getRecords()) {
                // As each TradingActivity is processed we create a new cTradingActivity object and add it to the TAList
                TAList.add(new cTradingActivity(ta));
           }
        //}
        return TAList;
    }*/
    
    
/*----------------------------------END OF WRAPPER CLASS-------------------------------------------------*/


 
 
/*---------------------------------SORTING METHOD FOR TA------------------------------------------------*/

         public String sortExpression {
        get {
            return sortExpr;
        }
        set {
            if(value == sortExpr)
                sortDirection = (sortDirection == 'ASC') ? 'DESC' : 'ASC';
            else
                sortDirection = 'ASC';
            sortExpr = value;
        }
    }
     public String getsortDirection() {
        if(sortExpression == null || sortExpression == '')
            return 'ASC';
        else
            return sortDirection;
    }
    
    public void setsortDirection(String value) {
        sortDirection = value;
    }
    
    public String getsortExpr() {
        return sortExpr;
    }
    
    public void setsortExpr(String value) {
        sortExpr=value;
    }
    
     public PageReference sortData() {
        sortFlag = true;
        getTALists();     
      //  con.setPageNumber(1);
     return null;  
    }
    
   
   
 /*--------------------------------END-SORTING--------------------------------------------------------*/  
 
/*------------------------------PAGINATION FOR TA----------------------------------------------------*/   
      
    public Boolean hasNext {
        get {
            return con.getHasNext();
        }
        set;
    }
      public Boolean hasPrevious {
        get {
            return con.getHasPrevious();
        }
        set;
    }
    
     public Integer pageNumber {
        get {
            return con.getPageNumber();
        }
        set;
    }
    public void first() {
       
        con.first();
        
    }
    
    public void last() {
        con.last();
    }
    
    public void previous() {
        con.previous();
    }
    
   public void next() {
        con.next();
    }
/*--------------------END-PAGINATION FOR TA-------------------------------------*/   

 

Best Answer chosen by Admin (Salesforce Developers) 
MSRMSR

Anyways i solved the issue myself by writing another controller and using passing value between pages feature......i hope this work around might solve who is facing the similar kivd of issue.