Simplest way to add checkboxes in Salesforce | Techila
1164
single,single-post,postid-1164,single-format-standard,ajax_updown_fade,page_not_loaded,,qode_grid_1300,footer_responsive_adv,hide_top_bar_on_mobile_header,qode-content-sidebar-responsive,qode-theme-ver-9.4.2,bridge,wpb-js-composer js-comp-ver-4.12,vc_responsive
 

Simplest way to add checkboxes in Salesforce

checkbox

Simplest way to add checkboxes in Salesforce

Simplest way to add checkboxes in Salesforce

Here I am presenting a blog on how to create checkboxes in visualforce. Firstly I have practiced a simple example of adding checkboxes in visualforce without using any Apex class or Wrapperclass. Let’s have a glimpse of it.

Here is the code I have written for adding checkboxes to VF page. I simply used the tag <apex:selectCheckboxes>.

 

<apex:page>

  <apex:form >

    <apex:pageBlock >

        <apex:pageBlockSection >

            <apex:pageBlockSectionItem> Skills:

             <apex:selectCheckboxes>

               <apex:selectOption itemLabel=”C” itemValue=”f”/>

                    <apex:selectOption itemLabel=”C++” itemValue=”cpp”/>   

                    <apex:selectOption itemLabel=”JAVA” itemValue=”j”/>

                    <apex:selectOption itemLabel=”HTML” itemValue=”h”/> 

                    <apex:selectOption itemLabel=”JAVASCRIPT” itemValue=”js”/>        

             </apex:selectCheckboxes>

            </apex:pageBlockSectionItem>

           </apex:pageBlockSection >

        </apex:pageBlock >

       </apex:form >  

</apex:page>

Description:http://www.techilaservices.com/techilla-oldsite/wp-content/uploads/2016/08/SimplestWayOfAddingCheckbox.png

 

As you can see in the output screenshot, checkboxes are showing in a row format. If change the layout in a tag <apex:selectCheckboxes>, we can achieve a good format of checkbox list.

Description: SimplestWayOfAddingCheckbox1.png

So this is the basic of adding checkboxes in a page. What if I want to list all Lead records with check boxes and in front of them, want to add “Delete” button on VF page top, where user will be able to perform Delete operation for all selected Leads. Here is the situation where the much popular in Salesforce “WrapperClass” can be use. A wrapper class is a custom object defined by programmer wherein he defines the wrapper class properties. Consider a custom object in salesforce, what do you have in it? Fields right? Different fields of different data types. Similarly wrapper class is a custom class which has different data types or properties as per requirement. We can wrap different objects types or any other types in a wrapper class.

So let’s start with writing a wrapper class in a developer console.

public class Sample2 {

    public List<WrapperClass> listWrapper {get;set;}

    public Boolean allBool {get;set;}

   

    public Sample2( ) {

        listWrapper = new List<WrapperClass>();

        List<Lead> listLead = [SELECT Name, SICCode__c FROM Lead];

        if(listLead.size() > 0) {

            for(Lead le : listLead) {

                listWrapper.add(new WrapperClass(le));

            }

        }

    }

   

    public class WrapperClass {

        public Boolean checked {get;set;}

        public Lead le {get;set;}

        public WrapperClass(Lead le) {

            this.le = le;

        }

    }

   

    public void del( ) {

        List<Lead> listEmpForDel = new List<Lead>();

        List<WrapperClass> listTempWrapper = new List<WrapperClass>();

        for(WrapperClass w : listWrapper) {

            if(w.checked) {

                listEmpForDel.add(w.le);

            } else {

                listTempWrapper.add(w);

            }

        }

        if(listEmpForDel.size() > 0) {

            delete listEmpForDel;

            listWrapper = listTempWrapper;

        } else {

            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR,’Select atleast one employee to delete’));

        }

    }

   

    public void selectAll() {

        if(allBool) {

            for(WrapperClass w : listWrapper) {

                w.checked = true;

            }

        } else {

            for(WrapperClass w : listWrapper) {

                w.checked = false;

            }

        }

    }

}

 

We have created a list of wrapper class by using which we can easily retrieve Lead object’s records. Here we have retrieved records based on Name and SIC Code.

Now, let’s write a visualforce page.

<apex:page controller=”Sample2″>

<apex:pagemessages />

    <apex:form >

        <apex:pageBlock >

           <apex:pageBlockTable value=”{!listWrapper}” var=”w”>

               <apex:column >

                   <apex:facet name=”header”>

                       <apex:inputCheckbox value=”{!allBool}”>

                           <apex:actionSupport reRender=”pg” action=”{!selectAll}” event=”onchange”/>

                       </apex:inputCheckbox>

                   </apex:facet>

                   <apex:inputCheckbox value=”{!w.checked}”/>

               </apex:column>

               <apex:column value=”{!w.Le.Name}”/>

               <apex:column value=”{!w.Le.SICCode__c}”/>

            </apex:pageBlockTable>

            <apex:pageBlockButtons >

                <apex:commandButton value=”Delete” action=”{!del}”/>

            </apex:pageBlockButtons>

        </apex:pageBlock>

    </apex:form>

</apex:page>

                            

 

Output:

Description: SimplestWayOfAddingCheckbox2.png

That’s all. 

Courtesyhttp://www.infallibletechie.com/2015/03/sample-wrapper-class-using-apex-in.html

Author: techila

No Comments

Post A Comment