I have the following search function:

public List<User> SearchResults {get; set;}

public ContactSearchController() 
            if(SearchResults == null)
                SearchResults = new List<User>();

public void runQuery() 
        for(User c: Database.query(soql))
    catch (Exception e) 
        ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'Ooops!'));


public PageReference runSearch() {

    String firstName = Apexpages.currentPage().getParameters().get('firstName');
    String lastName = Apexpages.currentPage().getParameters().get('lastName');
    //String technology = Apexpages.currentPage().getParameters().get('technology');

    soql = 'select id, department, name, title, division, Region_name__c, phone, mobilephone, email, IM_Skype__c from user where name != null';
    if (!firstName.equals(''))
        soql += ' and firstname LIKE \''+String.escapeSingleQuotes(firstName)+'%\'';
        if (!lastName.equals(''))
            soql += ' and lastname LIKE \''+String.escapeSingleQuotes(lastName)+'%\'';


    PageReference pageRef = new PageReference('/apex/SearchResults');
    return pageref.setRedirect(true);

Linked to the following VF page:

<apex:page controller=Controller" sidebar="false">
                <script type="text/javascript">

                 function doFunction(choice, ID)
                     switch (choice)
                      case "savemobile":
                      (document.getElementByID("Mobile").value, ID);
                      case "search":

<apex:form >
    <apex:pageMessages id="errors" />
      <apex:pageBlock title="Contact List" id="contactHeader" mode="edit">
      &nbsp;&nbsp;<b style="font-size: 15px;">Search</b>:&nbsp;
          <b style="margin-left: 25%;">First Name</b><input type="text" id="firstName" />

          <b>Last Name</b><input type="text" id="lastName"/>
                    &nbsp;&nbsp;<button type="button" onClick="doFunction('search', 'null');">Search</button>
            <apex:pageBlockButtons >

            <apex:commandButton action="{!updateAddresses}" value="Update Addresses" style="float:right;"/>


    <apex:actionFunction name="searchServer" action="{!runSearch}" rerender="results,debug,errors">
                      <apex:param name="firstName" value="" />
                      <apex:param name="lastName" value="" />
                    <apex:actionFunction name="saveMobile" action="{!Save}" rerender="results,debug,errors">
                      <apex:param name="mobileNumber" value="" />
                      <apex:param name="userID" value="" />

(with proper closing tags and such)

My unit test woks on the controller side (as in, the runSearch function fills in SearchResults with the appropriate User), but the List does not seem to persist on Page redirect to this page:

<apex:page controller="Controller" sidebar="false">

  <apex:form >
  <apex:pageMessages id="errors" />

  <apex:pageBlock title="Search Results" mode="edit">
 <apex:pageBlockButtons >
            <apex:commandButton action="{!ReturnToList}" value="Return to List" style="float:right;"/>

  <table width="100%" border="0">
    <td valign="top">

    <apex:pageBlock mode="edit" id="results">

        <apex:pageBlockTable value="{!SearchResults}" var="contact">

             <apex:column >
                <apex:facet name="header">
                    <apex:commandLink value="Team" action="{!toggleSort}" rerender="results,debug">
                        <apex:param name="sortField" value="department" assignTo="{!sortField}"/>
                <apex:outputField value="{!contact.Name}"/>

(again, with proper closing tags and such)

I believe that the new page is reloading the controller, and therefore is creating a new instance of SearchResults. Is there a way to make the SearchResults list persist across the page redirect?

  • 2
    In my code (visualforce wizard with many pages referencing one controller) i use public PageReference step2(){ return Page.Step2; } and it works fine. In your example you are using redirect=true and i think it flushes a whole view state. Feb 20, 2013 at 14:13

1 Answer 1


As @mast0r referenced in a comment on the first post, this is related to the fact that the redirect is set to true. Take a look at the PageReference class documentation:


Sets the value of the PageReference object's redirect attribute. If set to true, a redirect is performed through a client side redirect. 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.

  • Yeah I can see that now, thanks for pointing it out... I'm urnning into a problem where when I return Page.SearchResults, the search buttonclick event is not causing a page redirect to the Search Results Page... any insight on this?
    – gfppaste
    Feb 20, 2013 at 15:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.