0

This feels like a basic question but i am not able to get my head around it.

  1. have a vf page->controller.
  2. Controller has a search functionality
  3. VFpage needs token as parameter

Once a user clicks on the search button on the vf page, the page should be redirected to the same page showing the same search results and should have the token parameter that was passed to the vf page.

I have put the parameters and redirected in the search method, but the issue is that since it is a redirect of the page, it is executing the constructor and trying to query the results again.

I do not want to query the db again on refresh, i just want to keep the parameters as is.

Feels like i have come across this issue umpteen number of times but for now, i have gone blank :(. Please help.

1
  • As mentioned below,i also think that using rerender is the best option you have.Kindly close this question if your query has been answered.
    – Samir
    Nov 22, 2016 at 8:03

2 Answers 2

1

You can use the rerender attribute on your search button to prevent page refresh. In this case you don't want the whole page to refresh, just populate search results in vf page.

e.g.

<apex:outputpanel id="seachResultsPanel">
     Your results from controller
</apex:outputpanel>

Modify your button code

<apex:commandbutton value="Search" action="{!search}" rerender="seachResultsPanel" />
4
  • I can't believe i asked that question. I think i need a break !Thanks all!! :) According to the best practices, what is best to be returned by an action method? Is it void or PageReference(returning null) for the methods which intend to be redirected to the same page.
    – SF Learner
    Nov 22, 2016 at 9:58
  • Any action method you called will be redirect to same page unless you specify an another pagerefernce. So i suggest void.
    – Reshma
    Nov 22, 2016 at 10:09
  • Can you think of a use case that would justify having PageReference as return type and actually returning null(to redirect to the same page) as against using void for an action method?
    – SF Learner
    Nov 22, 2016 at 10:18
  • Basically both are same. If return null is using instead of void it will slow down a bit. Have a look at salesforce.stackexchange.com/questions/14168/…
    – Reshma
    Nov 22, 2016 at 10:34
1

You can rerender the page based on the search Text instead of reloading it.

Below is an example of it

VF Page

<apex:page title="Account Search" controller="AccountSearchController">
<apex:form> 
    <apex:inputext value="{!strAccountName}">
    <apex:outputpanel id="results">
        <apex:repeat value="{!lstAccounts}" var="Account">
            {!Account.Name} <br/>
        </apex:repeat>
    </apex:outputpanel>
    <apex:commandbutton value="Search" action="{!searchAccounts}" rerender="results"/>
</apex:form>

Controller

public class AccountSearchController() {

    /*Start - Global Variables*/
    Public String strAccountName        { get; set; }
    Public List<Account> lstAccounts    { get; set; }
    /*End - Global Variables*/

    /*Start - Constructor*/
    public AccountSearchController() {

        //Method to initialize variables
        init();
    }
    /*End - Constructor*/

    /*Start - Method searchAccounts to search Accounts Based on Name*/
    public void searchAccounts() {

        lstAccounts = Database.query('SELECT Id,Name FROM Account WHERE Name LIKE \'%'+strAccountName+'%\' ');
    }
    /*End - Method searchAccounts to search Accounts Based on Name*/

    /*Start - Method init to initialize variables*/
    Public void init() {

        strAccountName = '';
        lstAccounts = new List<Account>();
    }
    /*End - Method init to initialize variables*/
}

You must log in to answer this question.

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