2

I have a VFP that displays three different views from the same table, Case. I want to be able to remember the sort column and direction of each of the three views and using a view state seemed to be the right thing to do but I cannot get it to do anything.

Reading through An_Introduction_to_Visualforce_View_State (https://developer.salesforce.com/page/An_Introduction_to_Visualforce_View_State" target="_blank) it says that the View State contains "All non-transient data members in the associated controller (either standard or custom) and the controller extensions".

My controller has a number of String variables defined as public, one example is:

public String sortOrderNew {
        get  { if (sortOrderNew == null) {  sortOrderNew = 'CreatedDate'; } return sortOrderNew;  }
        set;
    }

I was expecting this value to be retained across visits the VFP that is linked to this controller but this does not happen. Every time I visit the page these public, non-transient variables are reset.

I did try and include a "public PageReference save()" function in the controller but the examples in the above document indicate that I should use "update" to save the data. Sadly this only works on sObjects, not on simple string values.

I would appreciate any suggestions of information you can provide on this. It shouldn't be this difficult.

[edited to provide more information]

It is a single page with a single controller. The page displays three apex:pageBlockTable tables and each table shows the data from a different SoQL statement. I have set up the VFP that changes the sort order like this:

<apex:column>
    <apex:facet name="header">
        <apex:commandLink value="Case #" action="{!toggleSortNew}" rerender="results,debug">
            <apex:param name="sortField" value="CaseNumber" assignTo="{!sortOrderNew}"/>
            <apex:outputPanel rendered="{!BEGINS(sortOrderNew,'CaseNumber')}">
                &nbsp;<apex:image value="{!IF(sortDirNew = 'desc','/img/arrowDown.gif','/img/arrowUp.gif')}"/>
            </apex:outputPanel>
        </apex:commandLink>
    </apex:facet>
    <apex:outputLink value="/{!case.Id}">{!case.CaseNumber}</apex:outputLink>
</apex:column>

When the column title is clicked it calls !toggleSortNew and sets sortOrderNew to 'CaseNumber'. different columns have the same toggle but set sortOrderNew to the field name of that column.

toggleSortNew looks like this:

public void toggleSortNew() {
    // Toggle the direction
    sortDirNew = sortDirNew.equals('asc') ? 'desc' : 'asc';
    // run the query again
    runQuery();
}

sortDirNew is defined in the Apex code as:

public String sortDirNew {
    get  { if (sortDirNew == null) {  sortDirNew = 'asc'; } return sortDirNew;  }
    set;
}

and sortOrderNew is coded as:

public String sortOrderNew {
    get  { if (sortOrderNew == null) {  sortOrderNew = 'CreatedDate'; } return sortOrderNew;  }
    set;
}

The runQuery() function simply creates the SoQL statement that retrieves the rows from the Case table, basing the "Order By" statement on the values of "sortOrderNew" and "sortDirNew".

I want to be able to store the values of "sortOrderNew" and "sortDirNew" so that when a user revisits the page their sort options are shown again, rather than having to reselect them.

I hope this more clearly explains what I am after.

1 Answer 1

3

Based on your updated question, you are misunderstanding the view state. Vew state is NOT maintained across user sessions / page visits. If you want to store sort order etc across sessions, page visits you will need to create a custom setting and store the values there in a hierarchy type list setting

Seeing how you implemented your page tags and the method that redirects between pages would be helpful but in general:

In order to maintain the view state across pages you need to ensure that ALL pages are using exactly the same controller and extensions. Also ensure that you do not have the redirect of the page reference = true.

Additionally, all pages must be on the salesforce.com domain. This means that view state cannot be maintained across public site pages as they are on the force.com domain.

This type of redirect performs an HTTP GET request, and flushes the view state, which uses POST. If set to false, the redirect is a server-side forward that preserves the view state if and only if the target page uses the same controller and contains the proper subset of extensions used by the source page.

Note that if the URL of the PageReference object is set to a website outside of the salesforce.com domain, or to a page with a different controller or controller extension, the redirect always occurs, regardless of whether the redirect attribute is set to true or false.

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_System_PageReference_setRedirect.htm

Additionally, if you are using properties to control the state of the table, you will need to ensure that you have properly constructed your subsequent pages to initialize using the properties as they are set and not default to a state assuming the variables are not set.

8
  • Sorry, I didn't want to just dump my code here, it's about 300 lines in all. Aug 17, 2016 at 14:08
  • and one-line comments don't help either. Aug 17, 2016 at 14:08
  • 2
    Sorry @Eric no, that was not meant to be rude or directed at you and I apologise for any potential affront caused. It was a statement regardng my own comment since I was going to provide more information within the comment itself but when I hit [Enter] it posted. It was a comment on my own inability to post comments, not on your assistance. Aug 17, 2016 at 14:31
  • 1
    @MartinW.Ward - Cool. you had me a bit confused there. I updated my answer. i believe you are misunderstanding the view state and what it does.
    – Eric
    Aug 17, 2016 at 14:33
  • 1
    Thanks very much for the explanation @Eric, and also for your understanding of my lack of manners. I will go look at custom settings then! Aug 17, 2016 at 14:50

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .