How can I retrieve the checkboxes that are selected (ticked or equal to true) on my VF page if these checkboxes were created dynamically?

The checkboxes are being displayed on my VF Page like this:

  <apex:selectCheckboxes value="{!choices}" onchange="CalcCheckBox(this)">  
    <apex:selectOptions value="{!listEvents}"/><br/>  

And these are my 2 methods used to generate the checkboxes:

//String array to keep track of the ids of choices  
public String[] choices { get; set; }

//listEvents of select options to populate select boxes  
public list<SelectOption> getlistEvents() {  
     list<SelectOption> options = new list<SelectOption>();  
       for (Presentation__c ow : [Select Id, Display_Name__c FROM Presentation__c WHERE (Status__c='Open' AND Date_of_Presentation__c >= TODAY) ORDER BY Date_of_Presentation__c ASC LIMIT 100]) {   
         options.add(new SelectOption(ow.Id, ow.miiEventV1__Display_Name__c));   
     return options;   

1 Answer 1


The choices string array will contain just the selected values, as if it were a multi select pick list not using a field. If the array is empty, no values were selected.


Here's a working demo:


public class selectdyn {
    public String[] values { get; set; }
    public SelectOption[] valueList { get; set; }

    public selectdyn() {
        valueList = new SelectOption[0];
        values = new String[0];
        for(Account record: [select id, name from account limit 100])
            valuelist.add(new selectoption(record.id, record.name));

    public string getOutputText() {
        return String.join(values, ',');


<apex:page controller="selectdyn">
    <apex:form id="form">
        <apex:selectCheckboxes value="{!values}">
            <apex:selectOptions value="{!valueList}"/>
        <apex:commandButton value="List Selections" reRender="form"/>
  • For some reason I am getting an error: Error: Conversion Error setting value 'a09N0000002R0dpIAC' for '#{choices}'.
    – manza
    Commented Jul 22, 2014 at 3:36
  • @manza I updated this answer with a properly working demo. I suspect it may have to do with trying to build the select list directly in a getter; if the values somehow change, I would expect some sort of validation error. If you need to alter your values, do so in an action method, and make sure you remove any invalid values from the list before returning.
    – sfdcfox
    Commented Jul 22, 2014 at 5:06
  • Thank you for the update, however in my page i have to create multiple groups of checkboxes, so how can I create a new group in the same page if this group display checkboxes from a new query?
    – manza
    Commented Jul 22, 2014 at 11:29
  • @manza Each needs to have its own array to choose from, so either you'll want multiple variables, a List<String[]> (list of list of strings), or you'll want a Map<String, String[]> (map of string to list of strings, although the key could be something else, like a number) so that each can have its own storage area.
    – sfdcfox
    Commented Jul 22, 2014 at 14:16
  • Can I ask you, I ma trying to do the test class but I am getting the following error, on the section of code where i add your solution: System.NullPointerException: Argument 2 cannot be null.
    – manza
    Commented Jul 29, 2014 at 6:05

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .