1
public class AccountSelectClassController1{

    //Our collection of the class/wrapper objects wrapAccount
    public List<wrapAccount> wrapAccountList {get; set;}
    public List<Account> selectedAccounts{get;set;}

    public AccountSelectClassController1(){
            String id = ApexPages.currentPage().getParameters().get('id');
            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() {
    PageReference congratsPage = Page.WrapperAccountEXChild;
    congratsPage.setRedirect(true);
       selectedAccounts = new List<Account>();
         for(wrapAccount wrapAccountObj : wrapAccountList) {
            if(wrapAccountObj.selected == true) {
                selectedAccounts.add(wrapAccountObj.acc);
                 congratsPage.getParameters().put('id', wrapAccountObj.acc.id);


            }
        }
    return congratsPage;
    }

    // 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;
        }
    }
}

Page 1 :

<apex:page controller="AccountSelectClassController1" 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="2">

                <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>

Page 2 :

<apex:page controller="AccountSelectClassController1">
  <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>
4
  • Why don't you use one page with 2 sections instead? You can render one or another either server side by using the reRender attribute, or you could do that with VisualForce remoting and some javascript (show/hide). Jul 16, 2015 at 7:18
  • Akash, it would probably help if you explain your code and how it's not doing what you expect or where you don't know how to continue. Jul 16, 2015 at 7:27
  • @Bachovski : I've tried the approch you suggested but my requirement is to display it on seperate page.
    – Akash
    Jul 16, 2015 at 7:29
  • Samuel : I'm accessing account records on page 1 and you can select any number of records using checkboxes (Wrapper class concept).I use one command button and set the page Reference to page 2 by onclick button.Now I want to display selected records of page 1 on page 2.I'm stuck with passing the id's of selected records to page 2 and thats why it's not showing any records but just displaying empty page block.
    – Akash
    Jul 16, 2015 at 7:32

1 Answer 1

3

Where multiple pages share the same controller the state established in one page is transferred to to the next page automatically. This allows things like multi-page wizards to be created.

But you have this setRedirect in your code:

congratsPage.setRedirect(true);

which:

This type of redirect performs an HTTP GET request, and flushes the view state, which uses POST. If set to false, the redirect is a server-side forward that preserves the view state if and only if the target page uses the same controller and contains the proper subset of extensions used by the source page.

So remove that line.

PS

This is all you should need in your method:

public PageReference processSelected() {
    selectedAccounts = new List<Account>();
    for (wrapAccount w : wrapAccountList) {
        if (w.selected) {
            selectedAccounts.add(w.acc);
        }
    }
    return Page.WrapperAccountEXChild;
}

with the second page able to access the view state created by the first page.

2
  • I'd appreciate if anyone can help me with that? I got stuck and couldn't solve the issue yet..Please post the suggested code or link. Thank you.!
    – Akash
    Jul 17, 2015 at 10:57
  • @Akash See the PS. If that doesn't work I'm not sure what is wrong.
    – Keith C
    Jul 17, 2015 at 11:43

You must log in to answer this question.

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