Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

I have the following search function:

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


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

public void runQuery() 
  {
    SearchResults.clear();
    try 
    {      
        for(User c: Database.query(soql))
        {
            SearchResults.Add(c);
        }
    } 
    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)+'%\'';

        }
        runQuery();
    }

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

Linked to the following VF page:

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

                 function doFunction(choice, ID)
                 {
                     switch (choice)
                     {
                      case "savemobile":
                      saveMobile
                      (document.getElementByID("Mobile").value, ID);
                      break;
                      case "search":
                      searchServer
                      (
                          document.getElementById("firstName").value,
                          document.getElementById("lastName").value
                      );
                      break;
                     }
                  }
                </script>
</head>

<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:pageBlockButtons>

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

(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;"/>

            </apex:pageBlockButtons>
  <table width="100%" border="0">
  <tr>  
    <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:commandLink>
                </apex:facet>
                <apex:outputField value="{!contact.Name}"/>
            </apex:column>

(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?

share|improve this question
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. –  mast0r Feb 20 '13 at 14:13

1 Answer 1

up vote 7 down vote accepted

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:

setRedirect

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.

share|improve this answer
    
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 '13 at 15:51

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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