3

I am not able to pass the data from one page to other page to display some values coming from first page to second page.

Visualforce Page1

<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:pageBlock>
</apex:form>
</apex:page>

Visualforce Page2

<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:pageBlock>
</apex:form>
</apex:page>

Apex Controller

public with sharing class wrapCustomObj {
public Integer limitSize=5;
public Integer OffsetSize=0;
public Integer totalRecs=0;
public List<employee__c> selectedEmp;


public List<employee__c> getSelectedEmp()
{
    selectedEmp=new List<employee__c>();
    if(selectedEmp.size()>0)
    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> wrpList{get;set;}
public List<wrapObj> selectedwrpList{get;set;}

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())
    {
        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;
    }
}

}

I am able to redirect to second page but i am not able to get the data from the first page. so it is displaying blanks rows and columns in pageBlocktable in second page. could any one figure me out where i am misleaded.

1
  • Your selectedwrpList is always empty since the wrapObj.selected = false
    – highfive
    Commented May 5, 2014 at 10:11

2 Answers 2

4

Your second page's getSelectEmpDetails method calls the getEmpDetails method which instead of just returning the existing wrpList which has the selected flags set in it, creates an entirely new list with the selected flags re-initialised to false.

A quick fix would be:

public List<wrapObj> getEmpDetails() {
    if (wrpList == null) {
        wrpList=new List<wrapObj>();
        List<Contact> emps=[select id,name,salary__c from employee__c
                where salary__c !=null limit:limitSize Offset:OffsetSize];;
        totalRecs=[select count() from Contact];
        for(Contact e:emps)
        {
            wrpList.add(new wrapObj(e,false));
        }
    }
    return wrpList;
}

But it is probably clearer (because it makes the connection between the get method and the property more obvious) to use the get/set capability of Apex's properties:

public List<wrapObj> wrpList{
    get {
        if (wrpList == null) {
            totalRecs = [select count() from Employee__c];
            wrpList = new List<wrapObj>();
            for(Employee__c e : [
                    select id, name, salary__c from employee__c
                    where salary__c != null
                    limit :limitSize
                    Offset :OffsetSize
                    ]) {
                wrpList.add(new wrapObj(e, false));
            }
        }
        return wrpList;
    }; 
    private set;
}

This "lazy load" approach (load it the first time it is referenced) works well for Salesforce controllers.

(You probably just haven't got to it yet, but your other methods such as next should return the appropriate PageReference too.)

Carrying state between pages by sharing a controller is a standard technique documented in e.g. Creating a Wizard.

0

When you are loading the second page, it'll call the controller from that particular page where all the fields will be re-initialized. So the values that you have set from the page 1 will not be visible to the page 2 since it's initializing a fresh controller instance.
Pass some fields (may be Ids) as URL parameters and work with them in the next page to overcome this. Below links may help you for this.
http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_System_PageReference_getParameters.htm https://developer.salesforce.com/forums?id=906F00000008sRlIAI

3
  • 1
    I think the code is trying to use the wizard pattern where because the pages share a controller the view state can be carried between the pages without using URL parameters.
    – Keith C
    Commented May 5, 2014 at 9:33
  • @highfive how can we pass List values of an Object to other page using URL ? I dont think it is possible to i am using the same controller for both pages Commented May 5, 2014 at 9:42
  • Agreed @KeithC. I think your point is valid here. @Gajini, When you are loading the 2nd page everytime it's having an empty list for selectedwrpList as per your controller since the wrapObj.selected flag is always false.
    – highfive
    Commented May 5, 2014 at 10:10

You must log in to answer this question.

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