public class AccountSelectClassController{ //Our collection of the class/wrapper objects wrapAccount public List wrapAccountList {get; set;} public List selectedAccounts{get;set;} public AccountSelectClassController(){ if(wrapAccountList == null) { wrapAccountList = new List(); 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 void processSelected() { selectedAccounts = new List(); for(wrapAccount wrapAccountObj : wrapAccountList) { if(wrapAccountObj.selected == true) { selectedAccounts.add(wrapAccountObj.acc); } } } // 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; } } } Please let us know if this will help you Thanks Amit Chaudhary", "upvoteCount": 1, "url": "https://trailhead.salesforce.com/trailblazer-community/feed/0D54V00007T4JWISA3", "datePublished": "2016-03-17T09:34:40.000Z", "author": { "@type": "Person", "name": "Amit Chaudhary", "url": "https://trailblazers.salesforce.com/profileView?u=0053000000AyQV3AAN", "affiliation": { "@type": "Organization", "name": "--" } } }, "suggestedAnswer": [ { "@type": "Answer", "text": "Hi Amit when am using the above code,am facing \"Compile Error: Invalid type: wrapAccount at line 3 column 13\". Please help.", "upvoteCount": 0, "url": "https://trailhead.salesforce.com/trailblazer-community/feed/0D54V00007T4JWISA3", "datePublished": "2016-03-17T10:47:25.000Z", "author": { "@type": "Person", "name": "Shruthi GM", "url": "https://trailblazers.salesforce.com/profileView?u=0053000000AT493AAD", "affiliation": { "@type": "Organization", "name": "--" } } } ] } } Skip to main content
Hi all,

I have a requirement to display all the accounts present on a vf page along with the checkboxes beside them so that user can select any no of accounts on the page.What is the code for this?

Please help.

Thanks inadvance.
11 answers
  1. Mar 17, 2016, 9:34 AM
    You can do this with the help of wrapper class. Please check below post. I hope that will help you

    1) http://www.sfdcpoint.com/salesforce/wrapper-class-in-apex/

    2) https://developer.salesforce.com/page/Wrapper_Class

    3) https://www.minddigital.com/what-is-wrapper-class-and-how-to-use-it-in-salesforce/

     

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

    </apex:pageBlock>

    </apex:form>

    </apex:page>

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

    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 void processSelected() {

    selectedAccounts = new List<Account>();

    for(wrapAccount wrapAccountObj : wrapAccountList) {

    if(wrapAccountObj.selected == true) {

    selectedAccounts.add(wrapAccountObj.acc);

    }

    }

    }

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

    }

    }

    }

    Please let us know if this will help you

    Thanks

    Amit Chaudhary
0/9000