+ Start a Discussion
ministe2003ministe2003 

Exceeding View State Limits despite StandardSetController pagination

Hi all,

I'm using a StandardSetController to hold lots of records but through pagination, only display a few at a time in a pageblocktable.

It's holding cases.  If I run a query which populates the SSC with several thousand cases I'm getting the Maximum view state size limit (135KB) exceeded errors.  The pageSize is set to just 20, so why am I getting this issue?!  I thought the whole point of using a SSC was to get around this issue?  It seems that even though my page size is 20, the view state is holding EVERY record in the SSC, which is several thousand and causing this issue.

 

Why am I getting this error despite using pagination?

 

Thanks

Best Answer chosen by Admin (Salesforce Developers) 
Ankit AroraAnkit Arora

Bob is right. All records will be stored in the view state, I have worked a lot over this and finally came to solution that restrict the query to a limit.

 

So limited records will be fetched out. Also you can make some fields transient which are of no use after used once.

 

I have a blog post regarding this, but not sure it will help you or not.

 

http://forceguru.blogspot.com/2010/11/best-practise-to-write-apex.html

 

 

 

Thanks

Ankit Arora

Blog | Facebook | Blog Page

All Answers

Shashikant SharmaShashikant Sharma

You can see your view state in browser ,

Enable for your user  : Edit your user record 

Development Mode : Set  it to true

Show View State in Development Mode : See  it to true

 

There you will see how each control is adding to view state,

 

Could you please share your code and the information from the view state as well if you can find some element which is taking big % of view state size?

ministe2003ministe2003

I have already done that and I can see from the view state that it is my StandardSetController of cases which is taking up a massive majority of the view state.  So the issue is even though the SSC's pageSize is set to 20, the view state is clearly holding the entire set of results (ie several thousand).

 

I thought the whole point of using an SSC was that the view state would only hold the number of records in the current page set?  am I correct in this assumption?

Shashikant SharmaShashikant Sharma

Yes Thats why we use SSC, I think you are querying (SOQL) in the constructor.

Could you please share your code? 

bob_buzzardbob_buzzard

Thats an interesting question, and one I hadn't really thought about before.  I've checked the docs and they don't state clearly one way or the other that I can find.

 

However, it makes sense that the records would be stored in the viewstate, otherwise any changes that you made to record while paging through them would be lost when you went to the next/previous page.

ministe2003ministe2003

That's what I'm wondering because I can't really see the point of Pagination if it's still limited to the viewstate size.

Shashikant , would running SOQL in the constructor make a difference in this case?  I do have queries in the constructor, but not any that are populating the cases SSC in question.

 

I'm not really sure what part of the code to share, its pretty massive...

Ankit AroraAnkit Arora

Bob is right. All records will be stored in the view state, I have worked a lot over this and finally came to solution that restrict the query to a limit.

 

So limited records will be fetched out. Also you can make some fields transient which are of no use after used once.

 

I have a blog post regarding this, but not sure it will help you or not.

 

http://forceguru.blogspot.com/2010/11/best-practise-to-write-apex.html

 

 

 

Thanks

Ankit Arora

Blog | Facebook | Blog Page

This was selected as the best answer
bob_buzzardbob_buzzard

Pagination makes sense for the user, in that they aren't flooded with informaiton.  Also for the visualforce rendering speed and the size of the HTML returned to the user - if you've ever tried to edit custom object definitions with 4-500 fields its pretty unresponsive.  While the viewstate increases with the number of records, the browser doesn't have to process and render every one of them when using pagination.

Shashikant SharmaShashikant Sharma

You can share some code, you constrcuctor and all the standard controller methods, I will try to mimic your case at my org and do a POC over it and will look to find a way also if no direct way , will update you over it. Just provide some bit of your code.

ministe2003ministe2003

Thanks for the feedback.  Disappointed to hear that the viewstate contains every record in an SSC, I've spent two days re-writing several VF pages with multiple tables to use pagination, and whilst they now look nicer, the functionality is not what I'd hoped for.  We have thousands of cases/contacts and hundreds of thousands of accounts and assets, so was hoping that by utilizing pagination I could give the user access to lots more records than they currently could with a standard list and table.  Disappointed to hear that isn't the case.

 

Thanks for the heads up though.

lkatneylkatney
Guys,

I found the same issue while working with standard set controller. Solution that fixed this problem is as follows:


/**
  Will add up all records to view state
*/
public ApexPages.StandardSetController setCon {
        get {
            if(setCon == null) {
                setCon = new ApexPages.StandardSetController(Database.query(
                    [SELECT Name, CloseDate FROM Opportunity]));
            }
            return setCon;
        }
        set;
    }


/**
  Will add up only first set of records to view state
*/
public ApexPages.StandardSetController setCon {
        get {
            if(setCon == null) {
                setCon = new ApexPages.StandardSetController(Database.getQueryLocator(
                    [SELECT Name, CloseDate FROM Opportunity]));
            }
            return setCon;
        }
        set;
    }

In first case in above example, I was using database.query and that was adding all records to View state. But when I changed to Database.getQueryLocator, it added only first set of  records to View state and that fixed my issue.

Hope this helps!!