1

I have the following page:

<apex:page controller="testPageController">
    <apex:form>
        <apex:selectCheckboxes value="{!selectedContacts}">
            <apex:selectOptions value="{!contacts}" />
        </apex:selectCheckboxes>

        <apex:commandButton value="Test" action="{!testAction}" />
    </apex:form>
</apex:page>

Controller:

public class testPageController {
    public List<SelectOption> contacts {get; set;}

    public List<String> selectedContacts {get; set;}

    public testPageController () {
        this.contacts = new List<SelectOption>();

        for (contact con : [
            SELECT Id, FirstName
            FROM Contact 
            LIMIT 10
        ]) {
            this.contacts.add(new SelectOption(con.Id, con.FirstName));
        }
    }

    public void testAction() {
        system.debug('Hello World!');
    }
}

Using apex version 43 in my developer org. Also reproduced in our companies sandbox org.

Steps to reproduce

  • preview page from dev console
  • select any number of options
  • click test
  • View Debug Logs

Expected

Hello World displays in logs

Actual

Nothing displayed in logs

End User Workaround

  • preview page from dev console
  • do not select any options
  • click test
  • View Debug Logs
  • Hello World displays
  • select any number of options
  • click test
  • View Debug Logs
  • Hello World displays

Can anyone reproduce this? Is this a bug or am I doing something wrong?

2

Your selectedContacts controller property doesn't contain an instance of a list after construction.

When you click the button, the VF page posts the data to the server which then tries to store your selections in a null list.

Initialize the list in the constructor and it works just fine.

public class testPageController {
    public List<SelectOption> contacts {get; set;}

    public List<String> selectedContacts {get; set;}

    public testPageController () {
        this.contacts = new List<SelectOption>();
        this.selectedContacts = new List<String>(); // need an instance of the list!

        for (contact con : [
            SELECT Id, FirstName
            FROM Contact 
            LIMIT 10
        ]) {
            this.contacts.add(new SelectOption(con.Id, con.FirstName));
        }
    }

    public void testAction() {
        system.debug('Hello World!');
    }
}
  • Strange you'd figure my apex:pageMessages tag would have displayed something for that (the one I have in the actual page, as the sample code obviously doesn't have one) – gNerb Jul 12 '18 at 22:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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