Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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;   
share|improve this question
up vote 5 down vote accepted

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(,;

    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"/>
share|improve this answer
For some reason I am getting an error: Error: Conversion Error setting value 'a09N0000002R0dpIAC' for '#{choices}'. – manza Jul 22 '14 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 Jul 22 '14 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 Jul 22 '14 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 Jul 22 '14 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 Jul 29 '14 at 6:05

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.