0

I have a form that contains two multi-select checkboxes in order to select certain days of the week. The first multi-select checkbox works completely fine and I can save the values. However, when I select boxes for the second set of checkboxes it prevents the page from being saved.

I have created the following test controller and page to mimic the issue-- with this example, neither of the checklists will let me save. The logs on my console do not show any errors, just a new log for the page after it refreshes when I press save. Why doesn't it forward the page when I save?

Controller:

public class TestVFPage_Controller {

public String[] weekdays1 {get; set;}
public String[] weekdays2 {get; set;}

   public PageReference save() {
        PageReference saved = new PageReference('/apex/TestVFSavePage');
        saved.getParameters().put('Weekdays1',String.valueOf(weekdays1));
        saved.getParameters().put('Weekdays2',String.valueOf(weekdays2));
        saved.setRedirect(true);
        return saved;
   }


   public List<SelectOption> getDayOptions() {
        List<SelectOption> weekdays = new List<SelectOption>();
        weekdays.add(new selectOption('Monday','Monday'));
        weekdays.add(new selectOption('Tuesday','Tuesday'));
        weekdays.add(new selectOption('Wednesday','Wednesday'));
        weekdays.add(new selectOption('Thursday','Thursday'));
        weekdays.add(new selectOption('Friday','Friday'));
        weekdays.add(new selectOption('Saturday','Saturday'));
        weekdays.add(new selectOption('Sunday','Sunday'));
        return weekdays;
    }



}

Visualforce:

<apex:page tabStyle="HigiConnector_HomePage__tab" controller="TestVFPage_Controller">
    <apex:form >
        <apex:pageBlock id="pb">
            <apex:pageBlockButtons >
                <apex:commandButton action="{!save}" value="Save" />
            </apex:pageBlockButtons>

            <apex:actionRegion >
                <apex:pageBlockSection columns="1">
                    <apex:pageBlockSectionItem >
                        <apex:outputLabel value="Checkbox 1" for="dayInvite" />
                        <apex:selectCheckboxes value="{!weekdays1}" id="dayInvite" layout="pageDirection">
                            <apex:selectOptions value="{!dayOptions}"></apex:selectOptions>
                        </apex:selectCheckboxes>
                    </apex:pageBlockSectionItem>

                     <apex:pageBlockSectionItem >
                        <apex:outputLabel value="Checkbox 2" for="refreshInvite" />
                        <apex:selectCheckboxes value="{!weekdays2}" id="refreshInvite" layout="pageDirection">
                            <apex:selectOptions value="{!dayOptions}"></apex:selectOptions>
                        </apex:selectCheckboxes>
                    </apex:pageBlockSectionItem>

                </apex:pageBlockSection>
            </apex:actionRegion>
        </apex:pageBlock>

    </apex:form>
</apex:page>
2

It looks like you are not instantiating the Lists to hold the selected values.

Add this to your controller:

public TestVFPage_Controller(){
    weekdays1 = New String[]{};
    weekdays2 = New String[]{};
}

This will instantiate the lists. If you had a page messages component on your page you would have saw the error:

enter image description here

Which would have alerted you to the error.

An alternate way of doing this would be:

public String[] weekdays1 = New String[]{};
public String[] weekdays2 = new String[]{};

public String[] getweekdays1(){
    return weekdays1;
}

public void setweekdays1(String[] weekdays1){
    this.weekdays1 = weekdays1;
}

public String[] getweekdays2(){
    return weekdays2;
}

public void setweekdays2(String[] weekdays1){
    this.weekdays2 = weekdays1;
}
  • Shouldn't they also be List<SelectOption>? – Adrian Larson Sep 22 '16 at 21:55
  • @AdrianLarson - I do not believe so as it stores the value not the select option object itself. Tested and working – Eric Sep 22 '16 at 21:58
  • Oh...I see it now. D'oh! – Adrian Larson Sep 22 '16 at 21:59
  • Hi Eric, thank you! I will start adding the page message component to my pages. It has been so frustrating not seeing any errors. And I now see what is happening. In my original code I have this public String[] inviteWeekdays {get; set;} and it works fine which has been really misleading. I probably instantiated the list somewhere in my code and have been overlooking it. Thank you again! – jalamak Sep 22 '16 at 22:04

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.