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
Sami ShakithSami Shakith 

Records are not showing while using wrapper class function in pagereference

Hi,

I am trying to filtering records using wrapper class from one VF page to another page.

here is VF page code
<apex:page standardController="Account" extensions="AccountSelectClassController" sidebar="false">
    <script type="text/javascript">
        function selectAllCheckboxes(obj,receivedInputID){
            var inputCheckBox = document.getElementsByTagName("input");
            for(var i=0; i<inputCheckBox.length; i++){
                if(inputCheckBox[i].id.indexOf(receivedInputID)!=-1){
                    inputCheckBox[i].checked = obj.checked;
                }
            }
        }
    </script>
    <apex:form >
        <apex:pageBlock >
            <apex:pageBlockButtons >
                <apex:commandButton value="Show Selected Accounts" action="{!processSelected}" rerender="table2"/>
            </apex:pageBlockButtons>
 
            <apex:pageblockSection title="All Accounts" collapsible="false" columns="1">
 
                <apex:pageBlockTable value="{!wrapAccountList}" var="accWrap" id="table" title="All Accounts">
                    <apex:column >
                        <apex:facet name="header">
                            <apex:inputCheckbox onclick="selectAllCheckboxes(this,'inputId')"/>
                        </apex:facet>
                            <apex:inputCheckbox value="{!accWrap.selected}" id="inputId"/>
                    </apex:column>
                    <apex:column value="{!accWrap.acc.Name}" />
                    <apex:column value="{!accWrap.acc.BillingState}" />
                    <apex:column value="{!accWrap.acc.Phone}" />
                </apex:pageBlockTable>
 
            </apex:pageblockSection>
        </apex:pageBlock>
    </apex:form>
 
</apex:page>

Below is my second VF page
 
<apex:page standardController="Account" extensions="AccountSelectClassController">
  <apex:form >
      <apex:pageBlock >
       <apex:pageBlockTable value="{!selectedAccounts}" var="c" id="table2" title="Selected Accounts">
                   
                   <apex:column value="{!c.Name}" headerValue="Account Name"/>
                   <apex:column value="{!c.BillingState}" headerValue="Billing State"/>
                   <apex:column value="{!c.Phone}" headerValue="Phone"/>
                </apex:pageBlockTable>
      </apex:pageBlock>
  </apex:form>
</apex:page>

Here is Controller Class
 
public class AccountSelectClassController{
 
    //Our collection of the class/wrapper objects wrapAccount
    public List<wrapAccount> wrapAccountList {get; set;}
    public List<Account> selectedAccounts{get;set;}
 
    public AccountSelectClassController(ApexPages.StandardController controller){
        if(wrapAccountList == null) {
            wrapAccountList = new List<wrapAccount>();
            for(Account a: [select Id, Name,BillingState, Website, Phone from Account limit 10]) {
                // As each Account is processed we create a new wrapAccount object and add it to the wrapAccountList
                wrapAccountList.add(new wrapAccount(a));
            }
        }
    }
 
    public pageReference processSelected() {
    selectedAccounts = new List<Account>();
 
        for(wrapAccount wrapAccountObj : wrapAccountList) {
            if(wrapAccountObj.selected == true) {
                selectedAccounts.add(wrapAccountObj.acc);
            }
        }
         pagereference ref;
     ref = new pagereference('/apex/AccountSelectedWrapper');
     ref.setredirect(true);
     return ref;
    }
 
    // This is our wrapper/container class. In this example a wrapper class contains both the standard salesforce object Account and a Boolean value
    public class wrapAccount {
        public Account acc {get; set;}
        public Boolean selected {get; set;}
 
        public wrapAccount(Account a) {
            acc = a;
            selected = false;
        }
    }
}

If give the second VF page function in 1st VF page its working fine. But displaying that filter record in another page means its showing none records. Simply displaying header alone. Please i need someone help.
Best Answer chosen by Sami Shakith
bob_buzzardbob_buzzard
This is because you have a rerender on your processSelected command button - that stops the browser navigating to the second page and updates the current page with the new viewstate.

All Answers

bob_buzzardbob_buzzard
I think the issue is this line in your processSelected() method:
ref.setredirect(true);
this carries out a client-side redirect that discards the viewstate and thus creates a brand new controller when the second page is rendered. If you remove this, the same controller instance will be used for both pages.

 
Sami ShakithSami Shakith
Thanks for your reply 

How can i retrive the records in second page?
bob_buzzardbob_buzzard
If you remove the redirect then the selectedAccounts property will be populated when you access the second page.
Sami ShakithSami Shakith

Its not redirecting to next page at one click. if i click 2 times its showing like that

"The page you submitted was invalid for your session. Please click Save again to confirm your change"

What should i do?

bob_buzzardbob_buzzard
This is because you have a rerender on your processSelected command button - that stops the browser navigating to the second page and updates the current page with the new viewstate.
This was selected as the best answer
Sami ShakithSami Shakith
Yeah its my mistake. Thank you so much bob.