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
Suresh RaghuramSuresh Raghuram 

can i pass a list of object from one vf page to another vf page

hi

 

I need to pass a list which consist of values retrieved by a query in one vf page and the page will be redirected to another page and there i need to show the all records in the list is it possible. if so can any body share process or example of a code.

 

Thanks in advance for your help.

Best Answer chosen by Admin (Salesforce Developers) 
Chamil MadusankaChamil Madusanka

You cannot pass list of object from one vf page to another as parameter passing. But there is a workaround for your requirement. You can share the controller for both vf pages. Use same controller class for both vf pages. Then you can redirect the page as follows

 

public PageReference nextPage()
{
			
	PageReference np = new PageReference('/apex/nextPage');
	np.setRedirect(false);
	return np;
}

 setRedirect(false) will keep your records in manipulated list. Then you can access that list of object from second page.

 

If a reply to a post answers your question or resolves your problem, please mark it as the solution to the post so that others may benefit.

All Answers

Suresh RaghuramSuresh Raghuram

no one come across such situation

Chamil MadusankaChamil Madusanka

You cannot pass list of object from one vf page to another as parameter passing. But there is a workaround for your requirement. You can share the controller for both vf pages. Use same controller class for both vf pages. Then you can redirect the page as follows

 

public PageReference nextPage()
{
			
	PageReference np = new PageReference('/apex/nextPage');
	np.setRedirect(false);
	return np;
}

 setRedirect(false) will keep your records in manipulated list. Then you can access that list of object from second page.

 

If a reply to a post answers your question or resolves your problem, please mark it as the solution to the post so that others may benefit.

This was selected as the best answer
Suresh RaghuramSuresh Raghuram

It is my code in VF Page1 conroller I cant able to see my records what is the mistake

 

List<Account> dupeRecord = [Select Id, FirstName, LastName from Account where FirstName =: account.FirstName OR LastName =: Account.LastName];
            if(dupeRecord.size() >0){
               pageReference dupePage = new pageReference('/apex/CustomerDuplicateRecord');
               dupePage.setRedirect(false);
               return dupePage;
               }

VF Page Code

<apex:page standardController="Account" tabStyle="Account" extensions="NewCustomerControllerExt">
    <apex:form >
        <apex:pageBlock >
            <apex:pageblocktable value="{!dupeRecord}" var="dup">
                <apex:column headerValue="First Name">
                <apex:outputField value="{!dup.FirstName}"/>
                </apex:column>
                <apex:column headerValue="Last Name">
                <apex:outputField value="{!dup.LastName}"/>   
                </apex:column>
</apex:pageblocktable>
</apex:pageBlock>
  </apex:form>
</apex:page>

Bryan HuntBryan Hunt

Chamil,

 

I am attempting to use your methodology for re-using the same controller instance for two VF pages so that I can access all of the same data on both pages.

 

But, when I bring up the second VF page, it appears to be generating a new instance of my controller object rather than referencing the instance used by the first page.

 

I used your 'nextPage' example exactly as shown.

 

How do I reference the existing controller instance?

 

Thanks.

 

Bryan Hunt

Chamil MadusankaChamil Madusanka

Can you post your calling method for both pages?

Bryan HuntBryan Hunt

Chamil,

 

Thank you for responding.

 

The first page is called from a custom button as a visualforce page: ClaimGenRMAPage[ClaimGenRMAPage]

 

The VF page references the controller as:

<apex:page standardcontroller="Claim__c" extensions="ClaimGenRMAController">

Controller code:

public class ClaimGenRMAController
{
    ...

 

    public ClaimGenRMAController(ApexPages.StandardController stdController) 
    {
        ...

    }   
    
    public ClaimGenRMAController() 
    {
    }
    
    public PageReference savePDF()
    {
        system.debug('Reason: ' + reason);
        system.debug('Comments: ' + comments);
        PageReference np = new PageReference('/apex/ClaimRMAPDFTemplate');
        np.setRedirect(false);
        return np;
    }
    
As you can see, I have two constructors, the second due to the second VF page.  The savePDF method is where I was trying to emulate your code.  Calling the second VF page.  The debugs confirm that there is valid data in the variables before doing the return.

 

The second VF page:

<apex:page controller="ClaimGenRMAController" renderas="pdf">
    <h1>Made it here</h1>
    <p>Reason: {!reason}</p>
    <p>Comments: {!comments}</p>
</apex:page>

 

Even though referencing the same controller, variables are empty.  I believe that the second VF page is spawning a new instance of the controller, rather than "inheriting" the original one.

 

Thanks for any help that you can give.

 

Bryan Hunt

Bryan HuntBryan Hunt

Chamil,

 

Your question about how the controller was called got me thinking.  The second VF page did not call the controller in exactly the same way (which was why it forced me to create a second constructor). 

 

I changed the invocation in the second VF page to match the first (and removed the dummy constructor) and the variable data was there!

 

Thanks for getting me thinking along the right path.

 

Bryan Hunt

vanessenvanessen

The wizard page works well but it won't work in the case you want the next page to be open in a new window, then you have

another option. To pass the list as a json string parameter to the other page and capture on the other side the json string  and then convert it back to the list. I've already done it several times.It works well

udduuddu

<apex:page controller="wrapperClassController1" >
<apex:form >
<apex:pageBlock >
<apex:pageBlockButtons >
<apex:commandButton value="Process Selected" action="{!processSelected}" rerender="table"/>
</apex:pageBlockButtons>
<!-- In our table we are displaying the cContact records -->
<apex:pageBlockTable value="{!contacts}" var="c" id="table">
<apex:column >
<!-- This is our selected Boolean property in our wrapper class -->
<apex:inputCheckbox value="{!c.selected}"/>
</apex:column>
<!-- This is how we access the contact values within our cContact container/wrapper -->
<apex:column value="{!c.con.Name}" />
<apex:column value="{!c.con.Email}" />
<apex:column value="{!c.con.Phone}" />
</apex:pageBlockTable>
</apex:pageBlock>
</apex:form>
</apex:page>

 

Controller:

 

public class wrapperClassController1 {
public String val { get; set; }


public List<Contact> disp { get; set; }

//Our collection of the class/wrapper objects cContact
public List<cContact> contactList {get; set;}
public List<Contact> contactLists {get; set;}
public void SetselectedContacts(List<Contact> c){
contactLists =c;
}

public List<Contact> getcontactLists(){
return contactLists;
}
List<Contact> selectedCon = new List<Contact>();
//This method uses a simple SOQL query to return a List of Contacts
public List<cContact> getContacts() {
if(contactList == null) {
contactList = new List<cContact>();
for(Contact c: [select Id, Name, Email, Phone from Contact limit 10]) {
// As each contact is processed we create a new cContact object and add it to the contactList
contactList.add(new cContact(c));
}
}
return contactList;
}

public PageReference processSelected() {

//We create a new list of Contacts that we be populated only with Contacts if they are selected
//We will cycle through our list of cContacts and will check to see if the selected property is set to true, if it is we add the Contact to the selectedContacts list
for(cContact cCon: getContacts()) {
if(cCon.selected == true) {
selectedCon.add(cCon.con);
System.debug('These are the selected Contacts...'+selectedCon);
}
}

this.SetselectedContacts(selectedCon);
// Now we have our list of selected contacts and can perform any type of logic we want, sending emails, updating a field on the Contact, etc
System.debug('These are the selected Contacts...');
PageReference pr = new PageReference('/apex/MyViewSelectedList');
pr.setRedirect(false);
for(Contact con: selectedCon) {
system.debug(con);
pr.getParameters().put('ContactsName',string.valueof(selectedCon[0].Name));
system.debug(selectedCon[0].Name+'hellofrom');
}
contactList=null; // we need this line if we performed a write operation because getContacts gets a fresh list now
disp =selectedCon;
val = 'contact';
System.debug(disp+'here is the list');
return Page.MyViewSelectedList;

}

public string getval(){
return 'contaact';
}
// This is our wrapper/container class. A container class is a class, a data structure, or an abstract data type whose instances are collections of other objects. In this example a wrapper class contains both the standard salesforce object Contact and a Boolean value
public class cContact {
public Contact con {get; set;}
public Boolean selected {get; set;}

//This is the contructor method. When we create a new cContact object we pass a Contact that is set to the con property. We also set the selected value to false
public cContact(Contact c) {
con = c;
selected = false;
}
}
}

 

VFpage 2 :

 

<apex:page controller="wrapperClassController1" >
<apex:form >
The contacts you selected are!!
<apex:pageBlock >
<apex:pageBlockTable value="{!disp}" var="c" id="table">
<apex:column value="{!val}"/>
<apex:column value="{!c.Name}" />
<apex:column value="{!c.Email}" />
<apex:column value="{!c.Phone}" />
</apex:pageBlockTable>
<apex:outputlabel >{!val}</apex:outputlabel>
<apex:outputLink >{!val}</apex:outputLink>
</apex:pageBlock>
<apex:outputLink >{!val}</apex:outputLink>

</apex:form>
</apex:page>

 

 

I am unable to get the details(List) to second VF page ..where is the problem..

 

THanks in advance

 

Uday. :)

varri jahnavivarri jahnavi
Hi Uday,

is this issue resolved..if so could you please share me how you were able to ..
Thank You