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
Nitin SharmaNitin Sharma 

Creating VF page with help of wrepper class

Scenario :
On Account there is checkbox named Active.
Build VF page that will show table with account details (fields).
There are 2 buttons on the same page i.e. Activate and Deactivate.
User will select some accounts from the given table and clicks Activate buttons and this makes Active checkbox true for selected accounts. Same will be the logic for Deactivate button for deactivating account.
Best Answer chosen by Nitin Sharma
Amit Chaudhary 8Amit Chaudhary 8
I have updated  the code according to your requirement and tested code in my developer org. Please try below code.
<apex:page controller="WrapperDemoController">
    <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 id="PB1">
            <apex:pageBlockButtons >
                <apex:commandButton value="Show Selected Accounts" action="{!processSelected}" rerender="table2,PB2"/>
            </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:column value="{!accWrap.acc.Active__c}" />
                </apex:pageBlockTable>
 
 
            </apex:pageblockSection>
        </apex:pageBlock>
        
        <apex:pageBlock id="PB2" >
            <apex:pageBlockButtons >
                <apex:commandButton value="Activate" action="{!ActivateData}" rerender="PB1,PB2"/>
                <apex:commandButton value="DeActivate" action="{!DeActivateData}" rerender="PB1,PB2"/>
            </apex:pageBlockButtons>

                <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:column value="{!c.Active__c}" headerValue="Active"/>
                </apex:pageBlockTable>
        </apex:pageBlock>

        
    </apex:form>

</apex:page>
public with sharing class WrapperDemoController {
    
    public List<wrapAccount> wrapAccountList {get; set;}
    public List<Account> selectedAccounts{get;set;}
 
    public WrapperDemoController ()
    {
            wrapAccountList = new List<wrapAccount>();
            searchRecord();
    }
    
    public void searchRecord()
    {
        wrapAccountList.clear();
            for(Account a: [select Id, Name,BillingState, Website, Phone ,Active__c from Account limit 10]) 
            {
                wrapAccountList.add(new wrapAccount(a));
            }
    }
 
    public void processSelected() 
    {
        
        selectedAccounts = new List<Account>();
        selectedAccounts.clear();
        for(wrapAccount wrapAccountObj : wrapAccountList) 
        {
            if(wrapAccountObj.selected == true) 
            {
                selectedAccounts.add(wrapAccountObj.acc);
            }
        }
    }

    public void ActivateData() 
    {
        for(Account acc : selectedAccounts )
        {
            acc.Active__c ='Yes';
        }
        update selectedAccounts ;
        searchRecord();
    }

    public void DeActivateData() 
    {
        for(Account acc : selectedAccounts )
        {
            acc.Active__c ='No';
        }
        update selectedAccounts ;
        searchRecord();
    }
    

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

}
User-added image


let us know if this will help you
 

All Answers

Amit Chaudhary 8Amit Chaudhary 8
I have updated  the code according to your requirement and tested code in my developer org. Please try below code.
<apex:page controller="WrapperDemoController">
    <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 id="PB1">
            <apex:pageBlockButtons >
                <apex:commandButton value="Show Selected Accounts" action="{!processSelected}" rerender="table2,PB2"/>
            </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:column value="{!accWrap.acc.Active__c}" />
                </apex:pageBlockTable>
 
 
            </apex:pageblockSection>
        </apex:pageBlock>
        
        <apex:pageBlock id="PB2" >
            <apex:pageBlockButtons >
                <apex:commandButton value="Activate" action="{!ActivateData}" rerender="PB1,PB2"/>
                <apex:commandButton value="DeActivate" action="{!DeActivateData}" rerender="PB1,PB2"/>
            </apex:pageBlockButtons>

                <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:column value="{!c.Active__c}" headerValue="Active"/>
                </apex:pageBlockTable>
        </apex:pageBlock>

        
    </apex:form>

</apex:page>
public with sharing class WrapperDemoController {
    
    public List<wrapAccount> wrapAccountList {get; set;}
    public List<Account> selectedAccounts{get;set;}
 
    public WrapperDemoController ()
    {
            wrapAccountList = new List<wrapAccount>();
            searchRecord();
    }
    
    public void searchRecord()
    {
        wrapAccountList.clear();
            for(Account a: [select Id, Name,BillingState, Website, Phone ,Active__c from Account limit 10]) 
            {
                wrapAccountList.add(new wrapAccount(a));
            }
    }
 
    public void processSelected() 
    {
        
        selectedAccounts = new List<Account>();
        selectedAccounts.clear();
        for(wrapAccount wrapAccountObj : wrapAccountList) 
        {
            if(wrapAccountObj.selected == true) 
            {
                selectedAccounts.add(wrapAccountObj.acc);
            }
        }
    }

    public void ActivateData() 
    {
        for(Account acc : selectedAccounts )
        {
            acc.Active__c ='Yes';
        }
        update selectedAccounts ;
        searchRecord();
    }

    public void DeActivateData() 
    {
        for(Account acc : selectedAccounts )
        {
            acc.Active__c ='No';
        }
        update selectedAccounts ;
        searchRecord();
    }
    

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

}
User-added image


let us know if this will help you
 
This was selected as the best answer
Nitin SharmaNitin Sharma
Thanks Amit, its working properly.