function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Jyoti NavaleJyoti Navale 

How Can I pass a List of Records from One VisualForce Page to another VisualForce Page?

Best Answer chosen by Jyoti Navale
Jim JamJim Jam
Both pages have to use the same controller, then you can navigate from one to the other by making sure you set the setRedirect parameter to false when returning the page reference for the second page. So, lets say you have a button on Page1 to take you to Page2. Page1 and Page2 use the same controller and the button on Page1 will call the controller method (gotoPage2) ...


public PageReference gotoPage2(){
  PageReference pageRef = new PageReference('/apex/Page2');
  pageRef.setRedirect(false);
  return pageRef;  
}

All Answers

Jim JamJim Jam
Both pages have to use the same controller, then you can navigate from one to the other by making sure you set the setRedirect parameter to false when returning the page reference for the second page. So, lets say you have a button on Page1 to take you to Page2. Page1 and Page2 use the same controller and the button on Page1 will call the controller method (gotoPage2) ...


public PageReference gotoPage2(){
  PageReference pageRef = new PageReference('/apex/Page2');
  pageRef.setRedirect(false);
  return pageRef;  
}
This was selected as the best answer
Jyoti NavaleJyoti Navale
But this is only redirection...How to pass record values?
Jaspreet S.Jaspreet S.
You Cannot pass list through URL, However If for both Pages you use same controller and you can store the records from one page to a variable, make that public with a getter & setter, and access the same variable from the second page, As both pages have the same Controller, So you can access that.If you have code, Please share I can update that, Thanks!!
Jyoti NavaleJyoti Navale
Thanks cmrc1001.3884211976265837E12 :)
Jyoti NavaleJyoti Navale
Here is my running code :

This is my first Apex Page, from where I just try to click on a button to load the records in my List variable

<apex:page Controller="SampleClass">
<apex:form >
    <apex:pageBlock title="Contact list" mode="edit">
    
    <apex:commandButton action="{!loadRecords}" value="Load Records"/>{!currentApexPage}
    </apex:pageBlock>
</apex:form>
</apex:page>


This is my second Apex page where I render the List of records.

<apex:page controller="SampleClass">
<apex:form >
<apex:pageBlock title="Contact list" mode="edit">
    <apex:pageBlockTable title="Table" value="{!conList}" var="record" rows="10" border="10px">
        <apex:column value="{!record.Id}">     </apex:column>
        <apex:column value="{!record.Name}">   </apex:column>
        <apex:column value="{!record.Email}">  </apex:column>
        <apex:column >
            <!-- <apex:outputlink value="/apex/SamplePage2"> Click Here to pass <apex:param name="id" value="{!record.id}"/> </apex:outputlink>
            <apex:commandButton value="Pass On to Next Page" action="{!click}"/>-->
        </apex:column>
    </apex:pageBlockTable>
</apex:pageBlock>
</apex:form>
</apex:page>

And Here is the common controller for both of above Apex pages

public class SampleClass{
    public String currentUrl {get; set;}
    public String currentApexPage {get; set;}
    public PageReference click() {
        return null;
    }

    public List<Contact> conList{get; set;}
    public SampleClass(){
        currentUrl = URL.getCurrentRequestUrl().toExternalForm();
        if(currentUrl.contains('SamplePage2')){
            loadRecords();

        }
    }
    public String getReferer()
    {
        return ApexPages.currentPage().getHeaders().get('referer');
    }
    public PageReference loadRecords(){
        conList = new List<Contact>();
        conList = [select id, name, email from Contact];
        PageReference pageRef = new PageReference('/apex/SamplePage2');
        pageRef.setRedirect(true);
        return pageRef; 
    }
}

Correct me if I am wrong. Thanks :)