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
bdbagleybdbagley 

Wrapper Class Example List Seleciton

I am completeley new to apex and coding in general so I was hoping I could get some help with a question.  I'm attempting to teach myself as much as I can by manipulating examples Wrapper Example so here we go.  I'm looking to do what I believe should be fairly simple: Take a list of accounts, from a wrapper class, that are selectable and then simply relist those selected in another column.

 

public class WrapperclassController {

    //Our collection of the class/wrapper objects cContact 
    public List<aAccount> AccountList {get; set;}

    //This method uses a simple SOQL query to return a List of Contacts
    public List<aAccount> getAccounts() {
        if(AccountList == null) {
            AccountList = new List<aAccount>();
            for(Account a: [select Id, Name, Phone from Account Order by Name]) {
                // As each contact is processed we create a new cContact object and add it to the contactList
                AccountList.add(new aAccount(a));
            }
        }
        return AccountList;
    }


    public PageReference processSelected() {

                //We create a new list of Contacts that we be populated only with Contacts if they are selected
        List<Account> selectedAccounts = new List<Account>();

        //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(aAccount aAcc: getAccounts()) {
            if(aAcc.selected == true) {
                selectedAccounts.add(aAcc.acc);
            }
        }

        // 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...');
        for(Account acc: selectedAccounts) {
            system.debug(acc);
        }
        return null;
    }


    // 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 aAccount {
        public Account acc {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 aAccount(Account a) {
            acc = a;
            selected = false;
        }
    }
}

 

<apex:page controller="WrapperclassController">
  <apex:sectionHeader title="Interview Three"/>
    <apex:form >
        <apex:pageBlock >
            <apex:pageBlockButtons >
                <apex:commandButton value="Show Selected Accounts" action="{!processSelected}" rerender="table"/>
            </apex:pageBlockButtons>
            <!-- In our table we are displaying the cContact records -->
        <apex:pageBlockSection title="All Accounts">
            <apex:pageBlockTable value="{!Accounts}" var="a" id="table">
                <apex:column >
                    <!-- This is our selected Boolean property in our wrapper class -->
                    <apex:inputCheckbox value="{!a.selected}"/>
                </apex:column>
                <!-- This is how we access the contact values within our cContact container/wrapper -->
                <apex:column value="{!a.acc.Name}" />
                <apex:column value="{!a.acc.Phone}" />
            </apex:pageBlockTable>
          </apex:pageBlockSection>
          
       <apex:pageBlockSection title="Selected Accounts">

        </apex:pageblocksection>
        
        </apex:pageBlock>
    </apex:form>
</apex:page>

So what I believe still needs to be done is possibly an action statement that takes the selected accounts withing the wrapper list and relists them in a new column on the visual force page but I am having trouble putting it together without errors.  I'd like the seleced accounts to be beside not below all accounts as well.

 

Any help would be appreciated as I am literally just getting into apex and coding as of about 3 weeks ago.

 

 

 

Best Answer chosen by Admin (Salesforce Developers) 
PremanathPremanath

All Answers

PremanathPremanath
This was selected as the best answer
bdbagleybdbagley

Wow that was exactly what I needed. Thanks for the quick help it saved me future headaches! Now I can manipulate it and learn some more.