0

I am trying to create a custom dependent picklist in Visualforce (I cannot use the standard field tags because the guest site user does not have access to the User object). I am using the selectOption tag as well as actionSupport to reference my method in a custom controller and I am stuck. I made sure there is a default value in the primary picklist. I've searched the boards and tried to make sure my actionSupport is correct. I also put the entire thing within the apex:actionRegion tag and that did not help. I had my developer friend take a look and he could not fix it either. The error I am getting is that I view the page, enter data and select an option within the first list. I then get an error that the apex action method must be a page reference. I don't actually know what that means or how to fix it. Here is my controller (at least the relevant part). VF page below.

    public with sharing class CommunitiesSelfRegController {
    /*** An apex page controller that supports self registration of users in communities that allow self registration */
    public String firstName {get; set;}
    public String lastName {get; set;}
    public String email {get; set;}
    public String phone {get; set;}
    public String password {get; set {password = value == null ? value : value.trim(); } }
    public String confirmPassword {get; set { confirmPassword = value == null ? value : value.trim(); } }
    public String communityNickname {get; set { communityNickname = value == null ? value : value.trim(); } }
    public String location {get; set{location = value;}}
    public List<SelectOption> customerOrgs{get;set;}
    public String selectedCustomerOrg {get; set;}

    public CommunitiesSelfRegController() {

    location='108 Charter Oak';
    getCustomerOrgs();
}

public void setCustomerOrgs(List<SelectOption> orgs) {
        this.customerOrgs = orgs;
    }

public List<SelectOption> getCustomerOrgs(){
    List<SelectOption> locs= new List<SelectOption>();
    if(location==null)
        return null;
    else
    {
        if(location.contains('Aerospace Elementary School')){
            locs.add(new SelectOption('Aerospace Elementary School','Aerospace Elementary School'));
            locs.add(new SelectOption('Other','Other'));
        }
        if(location.contains('108 Charter Oak')){
            locs.add(new SelectOption('Student Services', 'Student Services'));
            locs.add(new SelectOption('Other','Other'));
        }
        if(location.contains('147 Charter Oak')){
            locs.add(new SelectOption('Business Services','Business Services'));
            locs.add(new SelectOption('Facilities','Facilities'));
            locs.add(new SelectOption('Food Services','Food Services'));
            locs.add(new SelectOption('Human Resources','Human Resources'));
            locs.add(new SelectOption('Operations','Operations'));
            locs.add(new SelectOption('Other','Other'));
        }
        if(location.contains('155 Wyllys Street')){
            locs.add(new SelectOption('CREC Transporation','CREC Transporation'));
            locs.add(new SelectOption('Other','Other'));
        }
    } 
    return locs;
}

` Visualforce page (just the relevant part):

<apex:outputLabel value="Location" for="Location"/> <apex:actionRegion> <apex:outputPanel id="one"> <apex:selectList value="{!location}" title="Location" multiselect="false" size="3" onchange="{!Location}"> <apex:selectOption itemValue="Aerospace Elementary School" itemLabel="Aerospace Elementary School"/> <apex:selectOption itemValue="108 Charter Oak" itemLabel="108 Charter Oak"/> <apex:selectOption itemValue="147 Charter Oak" itemlabel="147 Charter Oak"/> <apex:selectOption itemvalue="155 Wyllys Street" itemLabel="155 Wyllys Street"/> <apex:actionSupport event="onchange" action="{!getcustomerOrgs}" reRender="two"/> </apex:selectList> <br/> <apex:outputPanel >Location: {!location}</apex:outputPanel> <br/> <apex:outputPanel id="two"> <b>Choose a Customer Organization</b> <apex:selectList size="1" value="{!customerOrgs}"> <apex:selectOptions value="{!CustomerOrgs}"/> </apex:selectList> </apex:outputPanel> </apex:outputPanel> </apex:actionRegion>

  • Please post the error message verbatim. – Adrian Larson Feb 2 '15 at 16:23
  • Return type of an Apex action method must be a PageReference. Found: core.apexpages.el.adapters.ApexListELAdapter – mpusto Feb 2 '15 at 16:57
1

Try renaming your method to something like loadCompanyOrgs and returning a PageReference as the error describes. It looks like you can't use a getter as an action.

public PageReference loadCompanyOrgs()
{
    // perform logic
    // return null (causes rerender)
}
  • I think OP can dispense with action= altogether as the getter referenced by output panel with id="two" should just use as lookup key the value set by the first selectList value=. OnChange with rerender will send the value of the selectList to the controller and rerender will cause page refresh. action method seems superfluous – cropredy Feb 3 '15 at 2:30
0

I think you might be able to move the id="two" to the select list. It was a little hard to test your code since it wasn't complete, but I was able to get it to work with this code.

Note, I would recommend storing the locations and orgs in custom objects and querying them instead of hard coding everything in code.

public class PicklistController {
    public String location {get; set;}
    public String selectedCustomerOrg {get; set;}

    public PicklistController() { }

    public List<SelectOption> getLocations() {
        List<SelectOption> locs= new List<SelectOption>();
        locs.add(new SelectOption('', '--Select Location--'));
        locs.add(new SelectOption('Aerospace Elementary School','Aerospace Elementary School'));
        locs.add(new SelectOption('108 Charter Oak', '108 Charter Oak'));
        locs.add(new SelectOption('147 Charter Oak', '147 Charter Oak'));
        locs.add(new SelectOption('155 Wyllys Street', '155 Wyllys Street'));
        return locs;
    }

    public List<SelectOption> getCustomerOrgs() {
        List<SelectOption> locs= new List<SelectOption>();
        locs.add(new SelectOption('', '--Select Organization--'));
        if (location != null) {
            if(location.contains('Aerospace Elementary School')) {
                locs.add(new SelectOption('Aerospace Elementary School','Aerospace Elementary School'));
                locs.add(new SelectOption('Other','Other'));
            }
            if(location.contains('108 Charter Oak')) {
                locs.add(new SelectOption('Student Services', 'Student Services'));
                locs.add(new SelectOption('Other','Other'));
            }
            if(location.contains('147 Charter Oak')) {
                locs.add(new SelectOption('Business Services','Business Services'));
                locs.add(new SelectOption('Facilities','Facilities'));
                locs.add(new SelectOption('Food Services','Food Services'));
                locs.add(new SelectOption('Human Resources','Human Resources'));
                locs.add(new SelectOption('Operations','Operations'));
                locs.add(new SelectOption('Other','Other'));
            }
            if(location.contains('155 Wyllys Street')) {
                locs.add(new SelectOption('CREC Transporation','CREC Transporation'));
                locs.add(new SelectOption('Other','Other'));
            }
        }
        system.debug(locs);
        return locs;
    }

    public Pagereference DummyCall() {
        return null;
    }
}

The page:

<apex:page controller="PicklistController" >
    <apex:sectionHeader title="Select your location" />
    <apex:form >
        <apex:pageMessages />
        <apex:pageBlock title="Location and Organization">
            <apex:pageBlockSection columns="1">
                <apex:pageblockSectionItem >
                    <apex:outputLabel value="Location"/>
                    <apex:outputPanel styleClass="requiredInput" layout="block">
                        <apex:outputPanel styleClass="requiredBlock" layout="block"/>
                        <apex:selectList size="1" value="{!location}">
                            <apex:selectoptions value="{!Locations}"/>
                            <apex:actionSupport event="onchange" rerender="org"/>
                        </apex:selectList>
                    </apex:outputPanel>
                </apex:pageblockSectionItem>
                <apex:pageblockSectionItem >
                    <apex:outputLabel value="Organization" />
                    <apex:outputPanel styleClass="requiredInput" layout="block">
                        <apex:outputPanel styleClass="requiredBlock" layout="block"/>
                        <apex:selectList size="1" value="{!selectedCustomerOrg}" id="org">
                            <apex:selectoptions value="{!customerOrgs}"/>
                        </apex:selectList>
                    </apex:outputPanel>
                </apex:pageblockSectionItem>
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>
  • With the custom object option, I thought that might work as well but it seems like in my case I'd need a junction object, as the orgs could belong to many locations. Is that what you were thinking? Also thank you for the thorough answer. Your code works beautifully. I can't get it to work within my page though. For some reason it is not re-rendering after selecting the first option. Could it be affected by my inputText fields? – mpusto Feb 5 '15 at 20:43
  • Try getting rid of public customerOrgs property and the setCustomerOrgs method. I think they might be confusing things. Also remove the action from your actionSupport tags. – Daniel Hoechst Feb 5 '15 at 22:52
  • Yes, you might need a junction object to link orgs to locations. – Daniel Hoechst Feb 5 '15 at 22:52

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.