Skip to main content

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

Change Port on a Spring Boot Application when using intelliJ

Change Port on a Spring Boot Application when using intelliJ  Two ways to change the port that the embedded Tomcat is using in a Spring Boot Application. The first way is to modify the application properties in the resources folder: Update application props in Spring Boot Application There are a couple issues with this approach.  You might only want this port change in your local environment and if you check it in by accident you might break things. Alternatively, you can use a command line parameter. Go to Run -> Edit Configurations -> VM options Then enter in the following argument: -Dserver.port=8090 Then click apply and OK and attempt to run the application again. Update port using VM option

How to set up a SQL Server 2008 Local Database

Setting up and Configuring your Microsoft SQL Server 2008 Local Database Download SQL Server Download SQL Server Express.  Express is the Free edition of MS SQL Server http://www.microsoft.com/en-us/download/details.aspx?id=1695 Install your SQL Server Instance Click the Start button -> Click Microsoft SQL Server 2008 -> Click Configuration Tools -> Select SQL Server Installation Center Select Installation -> Select New SQL Server stand-alone installation Select SQL Server stand-alone installation Get your Local SQL Server Service Up and Running Open up the SQL Server Configuration Manager Click the Start button -> Click Microsoft SQL Server 2008 -> Click Configuration Tools -> Select SQL Server Configuration Manager Setup SQL Server Config Manager Start up the SQL Services You want to ensure the following services are in the "Running" State and then you want to set the following services to automatic St...

Building Microservices with JHipster

Check out this article on Medium and LinkedIn Pulse, for a brief overview of microservices as a primer for working with JHipster to build web applications with a Microservices backend. Building microservices with Jhipster JHipster is sponsored by Ippon Technologies(https://www.ippon.tech/). Need cost-effective fractional engineering with US expertise? Fintech and Blockchain Specialized Fractional Engineering