Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

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/>  
  </apex:selectCheckboxes>  

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

1 Answer 1

up vote 3 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.

Edit

Here's a working demo:

Controller

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, ',');
    }
}

Page

<apex:page controller="selectdyn">
    <apex:form id="form">
        <apex:selectCheckboxes value="{!values}">
            <apex:selectOptions value="{!valueList}"/>
        </apex:selectCheckboxes>
        {!outputText}
        <apex:commandButton value="List Selections" reRender="form"/>
    </apex:form>
</apex:page>
share|improve this answer
    
For some reason I am getting an error: Error: Conversion Error setting value 'a09N0000002R0dpIAC' for '#{choices}'. –  manza Jul 22 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 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 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 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 at 6:05

Your Answer

 
discard

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.