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

How to maintain URL state?

Hi there,


Sorry, I am not sure if URL state is the right terminology here, but I will try to explain what I am trying to do here :)

1.  The user brings up the VF page (eg. MyPage) with a parameter in the URL: eg. http://site/apex/Page?e=12
2.  Based on the value of e, the controller of the VF page (eg. MyController) runs some logic and populates the VF page as part of its MyController() method.
3.  On MyPage, there is a set of output links (or ahrefs) with different values for e, eg.
4.  When the user clicks on these links, say, http://site/apex/Page?e=2, I would like to take the current e parameter ie. e=12 and append it to the e parameter that is selected and redirect the page back to itself as the URL: http://site/apex/Page?e=2+12, so that new data will be populated on MyPage based on now 2 different parameter values.  And then if the user clicks on http://site/apex/Page?e=3, I would like to take 3 and append it to the current url "state", and redirect back to MyPage with e=2+12+3, and so on, etc..


I figure I have to store the "state" of the url somewhere.  I am thinking I could have an onClick event on each of the separate links in Step 3, which updates a List or Map that stores the current state of the URL (ie. the running total of e's).  So I created a public List variable to store the running list of e's selected in MyController.  In my onClick method in the controller, I perform a PageReference in an attempt to modify the URL ... but the problem here is that if I use a PageReference.setRedirect back to the same page with the right e string (eg. 2+12), the URL of the page (and getParameters()) shows only e=2 (the last selected one), which means that the code in Step 2 to populate the page will keep painting it with data related to the last selected e (ie. 2)


How would you tackle this use case (storing url state)?  Any feedback is appreciated.


Message Edited by Iceman123 on 01-15-2010 09:52 AM
Best Answer chosen by Admin (Salesforce Developers) 

Turns out I was over-thinking.  My instructor pointed me to an easy solution using commandLink and param.


  <apex:commandLink action= ......>
               <apex:param name="firstParameter" assignTo="{!eVal}" value="11" />



All Answers


I'm not quite sure why you're doing something fancy with a redirect here. Why not actually set up the links the way you want them?


<apex:outputLink value="http://site/apex/Page?e="{!$CurrentPage.parameters.e}+2" >...</apex:outputLink>



Or am I missing something?

Message Edited by Avrom on 01-15-2010 11:19 AM
Message Edited by Avrom on 01-15-2010 11:20 AM

Thanks for the suggestion.  Actually, my app is a bit more complex.  It invokes a web service to perform a query against a database and the query is constructed based on the URL string (which in turn can be manipulated in many ways via different controls on the page based on user input). 


I think I just have to get past 1 hurdle - what is the recommended way to store a state such that the pagereference (returning null or back to itself) will not flush the state of the fields initialized on first load?  Here is an example:




public class TestAction {

public String eVal { get; set; }
public String urlString { get; set; }

public TestAction()
    system.debug('~~~~~~~~~ urlString on page load: ' + urlString);
public PageReference onClickMethod()
          urlString = eVal;
        system.debug('~~~~~~~~~ urlString before return to page: ' + urlString);
          return null;



 <apex:page controller="TestAction" tabStyle="Account" sidebar="false">
   <apex:form >

<a href="/apex/TestActionPage?e=11" onclick="clickJS('11');">Item 11</a>

     <apex:actionFunction action="{!onClickMethod}" name="clickJS" rerender="showstate" >
        <apex:param name="firstParam" assignTo="{!eVal}" value="" />



When the link for Item 11 is clicked,         system.debug('~~~~~~~~~ urlString before return to page: ' + urlString); correctly prints out the correct value of urlString before it returns to the same page.  But once it returns to the page, it flushes urlString and reinitializes it so that I cannot make use of the modified urlString.  


So what I am looking for is a way for it to not flush the state on page reload or some design guidance around how to go about preserving the "state" of these fields on page reloads.  Note:  This is not a wizard type of application though, so all interactions on the page will have to reload the same page.  I am not sure if this can only be done by using some type of session variable (which does not exist in salesforce, and I am trying to avoid having to store this state in client side cookies).



What about using an <apex:inputHidden> to pass on the current value of the parameter? That's generally how we recommend getting non-persistent values to cross requests.

Turns out I was over-thinking.  My instructor pointed me to an easy solution using commandLink and param.


  <apex:commandLink action= ......>
               <apex:param name="firstParameter" assignTo="{!eVal}" value="11" />



This was selected as the best answer