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">
<b style="font-size: 15px;">Search</b>:
<b style="margin-left: 25%;">First Name</b><input type="text" id="firstName" />
<b>Last Name</b><input type="text" id="lastName"/>
<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?
public PageReference step2(){ return Page.Step2; }
and it works fine. In your example you are usingredirect=true
and i think it flushes a whole view state.