0

I have a form in visualforce page. Once user fills out that form and hits 'Get Information' button, on the backend I am running a SOQL query to fetch leads and contacts based on the information entered by the user in the form and redirecting user on the second visualforce page. On the second visualforce page, I am trying to display the leads and contacts that I fetched via my SOQL query. I checked debug logs, it does return me the list of leads in SOQL query, but on the visualforce page it does not display anything. Also, once the leads and contacts are displayed, I want give user edit flexibility.

Here is my first visualforce page that has the form to fill in:

<apex:page controller="Test" standardstylesheets="false" showHeader="false">

<style>
body {
    <!-- background-image: url("http://www.w3schools.com/cssref/img_tree.gif"); -->
    background-repeat:no-repeat;
    background-color:#BFE5D9;

}


</style>

<div style="height:600px;width:600px;align:center">
<apex:form >
    <br/>
    <br/>
    <h1 align="center;"> Get Information </h1>
    <apex:pageBlock >


         <br/>
         <br/>


        <apex:pageBlockSection columns="1">
            <apex:inputText style="height:25px; width:200px; align:center; padding: 5px;" value="{!FirstName}" label="FirstName" /><br/><br/>

            <apex:inputText style="height:25px; width:200px; align:center; padding: 5px;" value="{!LastName}" label="LastName"/><br/><br/>


            <apex:inputText style="height:25px; width:200px; align:center; padding: 5px;" value="{!Birthdate}" label="DOB"/><br/><br/>

        </apex:pageBlockSection>


    </apex:pageBlock>
    <br/>
    <br/>
    <apex:commandButton style="width:400px; height:40px; color:green; background-color:white; font-size:25px; font-weight:bold" action="{!FindMatchingLeadAndContacts}" value="Get Information"/> 

</apex:form>
</div>

Here is the controller:

public class Test{

 public Test() {}
 public String FirstName;
 public String LastName;
 public Date  Birthdate;

 public list<Lead> leads {get;set;}
 public list<Contact> contacts {get;set;}

 public void SetFirstName(String Firstname)
   {
     this.FirstName = FirstName;
   }

 public String getFirstName()
   {
     return FirstName;
   }

 public void SetLastName(String Lastname)
   {
     this.LastName = Lastname;
   }

 public String getLastName()
   {
     return LastName;
   }

  public void SetBirthdate(Date Birthdate)
   {
     this.Birthdate = Birthdate;
   }

 public Date getBirthdate()
   {
     return Birthdate;
   }

public PageReference FindMatchingLeadAndContacts() {

    PageReference displayrecords = new pagereference('/apex/DisplayLeadsAndContacts');
    displayrecords.setRedirect(true);


    //List<sObject> result=new List<sObject>();

    List<lead> leads = new list<Lead>();
     leads = [SELECT id, Firstname, Lastname, FROM Lead where......];

    List<contact> contacts = new list<contact>();
     contacts= [SELECT id, Firstname, Lastname, Birthdate FROM Contact where ........];



    return displayrecords;
    }




 }

Here is the second visualforce page where I want to display teh fetched leads and contacts:

 <apex:page controller="Test" standardstylesheets="false" showHeader="false">
 <style>
body {

    background-repeat:no-repeat;
    background-color:#BFE5D9;

}
 </style>

 <apex:pageBlock >
   <apex:pageBlockSection title="Leads">
     <apex:pageBlockTable value="{!leads}" var="lead">
      <apex:column >

           <apex:facet name="header">First Name</apex:facet>
           <apex:outputText value="{!lead.FirstName}"/>
      </apex:column>
      <apex:column >

            <apex:facet name="header">Last Name</apex:facet>
            <apex:outputText value="{!lead.LastName}"/> 
      </apex:column>
    </apex:pageBlockTable>
 </apex:pageBlockSection>

    <apex:pageBlockSection title="Contacts">
   <apex:pageBlockTable value="{!contacts}" var="contact">
      <apex:column value="{!contact.FirstName}"/>
      <apex:column value="{!contact.LastName}"/>
    </apex:pageBlockTable>
 </apex:pageBlockSection>
</apex:pageBlock>

</apex:page>

Thanks

2
  • Generally it's much easier to do everything in a single Visualforce page and use controller variables to hide/display page sections as needed. That way you don't have to pass data between pages, Oct 24, 2019 at 23:53
  • yes but I am using the same controller for both visualforce pages. Would it still not be able to access the leads and Contacts returned by the SOQL query in controller?
    – Anonymous
    Oct 25, 2019 at 0:27

1 Answer 1

1

Calling setRedirect(true) clears the view state, meaning that your second page has no access to the data accumulated by the first page:

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.

While your pages could be rearchitected into a single page that conditionally presents different elements based on its state, you can make this work with the current implementation by not using that type of redirect, since the two pages do share a controller.

2
  • Thank you for your help. I tried changing it to setRedirect(false), it didn't go to the next page at all and stays on the same page :(
    – Anonymous
    Oct 25, 2019 at 15:45
  • Even if I try to call it on same page below the button just to test, it still does not display anything. I think there might be something wrong with the way I am trying to display leads and contacts on the page.
    – Anonymous
    Oct 25, 2019 at 16:04

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.