Salesforce Dynamically Populated List of Checkboxes in Visualforce and Apex



Dynamically Populated List of Checkboxes in Visualforce and Apex



Below you will find details on how to create a dynamic checkbox group on a custom Salesforce page using Apex and Visualforce.

These examples will leverage Visualforce specific tags and Apex specific functionality to accomplish things that you would normally need an external library such as jQuery to acheive.



Dynamic Array of Checkboxes


Dynamically build an array of checkboxes and keep track of which boxes are checked.  Checkboxes will be populated from a SOQL query from a custom object in Salesforce.  Selected options will be tracked and persisted if you re-render the checkboxes.



Visualforce


Command button to refresh list of checkboxes

  <apex:commandButton action="{!refresh}" reRender="customCheckboxPanel" status="loadingDetails">  

This is just an example.  A more practical use of this would be a onchange or onclick event such as if you had checkbox groups on multiple tabs.


Output panel that is refreshed to display dynamic list of checkboxes

  
<apex:outputpanel id="customCheckboxPanel" layout="block">  
                       <apex:actionStatus id="loadingDetails"   
                           startText="Loading..."   
                           stopText=""/>    
                             <br/>  
                             <apex:selectCheckboxes value="{!choices}" layout="pageDirection">  
                               <apex:selectOptions value="{!list}"/><br/>  
                             </apex:selectCheckboxes>  
</apex:outputpanel>  



Apex


This code will go in your controller.

You will have an array of strings that will contain each id of each selected option.

You will also need a method that retrieves the options via a SOQL queries and loads them into selectionOption objects

Choice Array and Get Select Options Method

 
//String array to keep track of the ids of choices  
public String[] choices { get; set; }
  
//List of select options to populate select boxes  
public List<SelectOption> getList() {  
     List<SelectOption> options = new List<SelectOption>();  
       for (Your_Custom_Object__c ow : [  
         SELECT Name,id FROM Your_Custom_Object__c]) {   
         options.add(new SelectOption(ow.id, ow.Name));   
       }    
     return options;   
}   

The id is being set as the value and the Name is being set as the displayed label.  Replace Your_Custom_Object__c with the name of the custom object you want to query.

You should now have a set of checkboxes. Checked options will automatically be tracked in the choices String array.


Checkbox Array Example
Check boxes populated with a SOQL query
Dynamically populated checkboxes




Software Development Blogs - BlogCatalog Blog Directory

Popular posts from this blog

How to set up a SQL Server 2008 Local Database

Spring Boot Internationalization with Default Locale for Message Strings

Change Port on a Spring Boot Application when using intelliJ