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
Sandeep M 1Sandeep M 1 

Passing List values of an object to other vf page

Hello all,

I want to pass the List values of an object to the other Visualforce page using a query. Since we cannot pass list as parameter to other page i am using

public PageReference DisplayDetails() {
      
        PageReference reRend = new PageReference('/apex/wrapperCustomObjpage2');
        reRend.setRedirect(false);
        return reRend;
       
    }

But the variables in the controller were reintializing after entering into the other page. I am not able to display the data into pageblocktable. Could any one suggest me the best approach
Vinit_KumarVinit_Kumar
Your question is not clear.Can you elaborate  as what's happening ??
Sandeep M 1Sandeep M 1
Sure Vinit_Kumar !

There are two pages lets say page1 and page 2 both were using same controller. In first page i am selecting records from an Object. Now i have to display the selected records in second object. I am able to get the expected logs for the first page but when i click on display button which navigates to next page. The variable are getting reintialized and i am not able to display the selected records in second page


Page 1 :

<apex:page controller="wrapCustomObj">
    <apex:form >
        <apex:pageBlock title="Employee Details">
            <apex:pageBlockSection title="List of Employee" id="details">
                <apex:pageBlockTable value="{!empDetails}" var="e">
                    <apex:column >
                        <apex:inputCheckbox value="{!e.selected}"/>
                    </apex:column>
                    <apex:column value="{!e.empDet.name}"/>
                    <apex:column value="{!e.empDet.Salary__c}"/>
                </apex:pageBlockTable>
            </apex:pageBlockSection>
            <apex:pageBlockButtons location="bottom">
                <apex:commandButton action="{!previous}" value="Previous" reRender="details"/>
                <apex:commandButton action="{!next}" value="Next" reRender="details"/>
                <apex:commandButton action="{!fPage}" value="First Page" reRender="details"/>
                <apex:commandButton action="{!lPage}" value="Last Page" reRender="details"/>
            </apex:pageBlockButtons>
            <apex:pageBlockButtons location="bottom">
                <apex:commandButton action="{!DisplayDetails}" value="Details"/>
            </apex:pageBlockButtons>
            <apex:outputLabel >{!SelectedEmp}</apex:outputLabel>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Page 2 :

<apex:page controller="wrapCustomObj">
    <apex:form >
        <apex:pageBlock >
            <apex:pageBlockTable value="{!SelectEmpDetails}" var="e">
                <apex:column value="{!e.empDet.name}"/>
                <apex:column value="{!e.empDet.Salary__c}"/>
            </apex:pageBlockTable>
            <apex:outputPanel >
                <apex:outputLabel >"{!SelectEmpDetails}"</apex:outputLabel>
            </apex:outputPanel>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Controller :

public with sharing class wrapCustomObj {
    public Integer limitSize=5;
    public Integer OffsetSize=0;
    public Integer totalRecs=0;
    public List<employee__c> selectedEmp=new List<employee__c>();
    public List<wrapObj> wrpList{get;set;}
    public List<wrapObj> selectedwrpList{get;set;}

    public List<employee__c> getSelectedEmp()
    {
       
        if(selectedEmp.size()>0)
        {
            System.debug(' selectEmp '+selectedEmp);
            return selectedEmp;
        }
        else
        return null;
    }
    public void lPage() {
       OffsetSize=totalrecs-math.mod(totalRecs,limitSize);

    }
    public void fPage() {
        OffsetSize=0;
    }
    public void next() {
        OffsetSize=OffsetSize+limitSize;
     }
    public void previous() {
         OffsetSize=OffsetSize-limitSize;
    }
    public PageReference DisplayDetails() {
        PageReference reRend = new PageReference('/apex/wrapperCustomObjpage2');
        reRend.setRedirect(false);
        return reRend;
     }

    public List<wrapObj> getEmpDetails() {
        wrpList=new List<wrapObj>();
        List<Employee__c> emps=[select id,name,salary__c from employee__c where salary__c !=null limit:limitSize Offset:OffsetSize];
        totalRecs=[select count() from Employee__c];
        for(Employee__c e:emps)
        {
            wrpList.add(new wrapObj(e,false));
        }
        return wrpList;
    }
    public List<wrapObj> getSelectEmpDetails()
    {
        selectedwrpList=new List<wrapObj>();
        for(wrapObj w:getEmpDetails())
        {
             System.debug(' selectEmDetails '+w);
            if(w.selected==true)
            selectedwrpList.add(w);
        }
      
        return selectedwrpList;
    }
    public class wrapObj
    {
        public Employee__c empDet{get;set;}
        public Boolean selected{get;set;}
        public wrapObj(Employee__c emp,boolean check)
        {
            empDet=emp;
            selected=check;
        }
    }

}
Vinit_KumarVinit_Kumar
I see you are fetching record using getter method.Hence,whenever the page would be reloaded it would reintialize the variable.

The way I would approach is I would create a custom method and then fetch that list in that method and then I would call that method in 2nd page as  apex:page action attribute somethiing like below :

<apex:page controller="customController" action = "{!custommethod}">
Sandeep M 1Sandeep M 1
I solved this issue by changing pagereference method to

public PageReference DisplayDetails() {
        selectedwrpList=new List<wrapObj>();
        for(wrapObj emp:wrpList)
        {
            if(emp.selected == true) {
            selectedwrpList.add(emp);
            }
        }
        PageReference reRend = new PageReference('/apex/wrapperCustomObjpage2');
        reRend.setRedirect(false);
        return reRend;
       
    }

Now i am able to display the page2 data correctly . But now , the issue is the url is not changing to wrapperCustomObjpage2.
Vinit_KumarVinit_Kumar
You are setting setRedirect() method to false,hence the page URL is not getting changed.

Change it to true like


reRend.setRedirect(true);

It should change the URL
Sandeep M 1Sandeep M 1
if i use setRedirect(true) the values are not passing again.
Vinit_KumarVinit_Kumar
Use the pagereference method in the action attribute of 2nd page ,that should work then ,something like below :-

<apex:page controller="customcontroller" action="{!DisplayDetails}" >
Sandeep M 1Sandeep M 1
Visualforce Error

Cyclical server-side forwards detected: /apex/wrapperCustomObjpage2
varri jahnavivarri jahnavi
hi All,

I am also working on same scenario..are you able to overcome this issue?
varri jahnavivarri jahnavi
Hi Sandeep, 

Are you able to achieve the output which you were expecting..if so could you please share the code patch..It would be of great help for me, as i am also working on similar task..

Thank you
Deepu SandeepDeepu Sandeep
i used two controllers instead of that by sending parameters to other page .
varri jahnavivarri jahnavi
Hi Sandeep,

I am facing this issue-


Sandeep M 1
Sure Vinit_Kumar !

There are two pages lets say page1 and page 2 both were using same controller. In first page i am selecting records from an Object. Now i have to display the selected records in second object. I am able to get the expected logs for the first page but when i click on display button which navigates to next page. The variable are getting reintialized and i am not able to display the selected records in second page

Could you share your code how you were able to resolve will be of great help.

Thank You
Sandeep M 1Sandeep M 1
Hi Jahnavi , I done some thing like this,
this is the trick
            PageReference newPAge = page.eStatementPdf;
            newPage.getParameters().put('id', value);
            newPage.getParameters().put('empNumber', ename);
            return newPage;


Page 1 Vf:

<apex:page Controller="eStatementController">
  <apex:form >
      <apex:pageBlock >
          <apex:pageBlockSection title="Bank E-Statement" columns="1">
               <apex:inputField value="{!bStatement.Bank_Account__c}"/>
               <apex:inputField value="{!bStatement.Start_Date__c}"/>   
               <apex:inputField value="{!bStatement.End_Date__c}"/>
               <apex:inputField value="{!bStatement.Currency__c}"/>
               <apex:inputField value="{!bStatement.Organization__c}"/>
               <apex:pageBlock >
                   <apex:pageBlockButtons location="bottom">
                       <apex:commandButton value="Generate Statement" action="{!generate}" />
                   </apex:pageBlockButtons>
               </apex:pageBlock>
          </apex:pageBlockSection>
      </apex:pageBlock>

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

Page1 controller :

public with sharing class eStatementController {
    public String orgname { get; set; }
    List<Bank_statement__c> bsList=new List<Bank_statement__c>();
    public PageReference generate() {
            insert bStatement;
             System.debug('state '+bStatement);
            List<bank_account__c> ba= [select id,name,employee__r.name,employee__r.Employee_Number__c from bank_account__c where id=:bStatement.bank_account__c];
            List<employee__C> emp=[select id,organization__r.name from employee__c where employee_number__c =:ba[0].employee__r.Employee_Number__c];
                      
            if((emp[0].organization__r.id==bstatement.organization__c) && (ba[0].id==bStatement.Bank_Account__c))
            {
            Id value = ba[0].id;
            String ename=ba[0].employee__r.Employee_Number__c;
            PageReference newPAge = page.eStatementPdf;
            newPage.getParameters().put('id', value);
            newPage.getParameters().put('empNumber', ename);
            return newPage;
            }
            else
            {
                return null;
            }
    }
    public Bank_Statement__c bStatement{get;set;}
    public eStatementController()
    {
        bStatement=new Bank_Statement__c();

    }
}

Page 2 vf :

<apex:page controller="eStatementPdfController" renderAs="pdf">
     <apex:pageBlock >
         <apex:pageBlockSection columns="2">
            <apex:pageBlockSectionItem >
                <apex:outputPanel >
                     <h2><b>{!empName}</b></h2>
                     <p>{!orgName}<br/>
                     {!address}</p>
                </apex:outputPanel>
            </apex:pageBlockSectionItem>
           <apex:pageBlockSectionItem >
               <apex:column value="a" headerValue="Account type"/>
                  <apex:column value="b" headerValue="Balance"/>
                  <apex:column value="c" headerValue="Currency"/>
                </apex:pageBlockTable> -->
            </apex:pageBlockSectionItem>
         </apex:pageBlockSection>
     </apex:pageBlock>
</apex:page>

Page2 Controller :

public with sharing class eStatementPdfController {
    public List<bank_statement__c> accountDetails { get; set; }
    public String address { get; set; }
    public organization__c orgs { get; set; }
    public string empname{get;set;}
    public string orgname{get;set;}
    Public String param_value1{get;set;}
    Public String param_value2{get;set;}
    List<Employee__c> org=new List<Employee__c>();
    List<bank_statement__c> bs=new List<bank_statement__c>();
    public eStatementPdfController()
    {
        accountDetails = new List<bank_statement__c>();
        param_value1 = ApexPages.currentPage().getParameters().get('id');
        System.debug('p 1 '+param_value1);
        param_value2 = ApexPages.currentPage().getParameters().get('empNumber');
        System.debug('p 2 '+param_value2);
        org=[select id,name,organization__r.name,organization__r.address__c from employee__c where employee_number__c=:param_value2];
        orgname=org[0].organization__r.name;
        empname=org[0].name;
        address=org[0].organization__r.address__c;
      
    }
}


varri jahnavivarri jahnavi
Hi Sandeep,

Thank you so much for the quick turnaround..But I am looking for pagination and selected records in first page needs to be displayed in second page...Will this code handle this too..Kindly let me know


varri jahnavivarri jahnavi
Sure I will. Thanks Sandeep..