3

I want to dynamically show a picklist depending on the selection of another picklist so the user can enter a required field for a particular record type.

VF Code

<apex:page StandardController="Contact" extensions="AKF_Save_Contact">
    <apex:form id="form">
        <apex:pageBlock >
            <apex:pageBlockButtons>
                <apex:commandButton value="Next" action="{!saveContact}" />
            </apex:pageBlockButtons>
            <apex:pageBlockSection columns="1">
                <apex:pageBlockSectionItem>
                    <apex:outputLabel value="First Name: " for="firstName"/>
                    <apex:inputField id="firstName" value="{!c.firstName}" />
                </apex:pageBlockSectionItem>
                <apex:pageBlockSectionItem>
                    <apex:outputLabel value="Last Name: " for="lastName"/>
                    <apex:inputField id="lastName" value="{!c.LastName}" />
                </apex:pageBlockSectionItem>
                <apex:pageBlockSectionItem>
                    <apex:outputLabel value="Record Type: " for="recordType"/>
                    <apex:selectList size="1" value="{!selectedValue}">
                        <apex:actionSupport event="onchange" action="{!checkForIndustry}" reRender="industryRole1"/>
                        <apex:selectOptions value="{!recordTypes}"/>
                    </apex:selectList>
                </apex:pageBlockSectionItem>
                <apex:outputPanel id="industryRole1">
                    <apex:pageBlockSectionItem rendered="{!industrySelected}" >
                        <apex:outputLabel value="Industry Role: " for="industryRole" />
                        <apex:inputField id="industryRole" value="{!c.Industry_Role__c}" />
                    </apex:pageBlockSectionItem>
                </apex:outputPanel>
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
 </apex:page>

Controller method

public void checkForIndustry () {
    System.debug('selectedValue----'+selectedValue);
    if (selectedValue == 'Industry') {
        industrySelected = true;
    }
}

The debug statement in the method never fires. I do get an log in the dev console with no debug statements. The "Next" button works. What is wrong?

3

Okay! Finally have a real, valid solution. So when you call an action, either through actionSupport or actionFunction with an onchange, you submit your page, which determines the data inside your controller, which is used by your code.

The issue here was that you needed to be really specific about what data is being submitted- you don't want your contact data to be submitted, because it could be partially filled out, and fail your validation.

If you had added & setup a rerender pointing at a apex:pageMessages, you might have caught this a lot quicker, it'll say "Required fields missing", because the fields are likely empty when the page is submitted.

You can get around this either through immediate="true", which will call the action without submitting the view state, but we need to pass a variable along with the action. We could mess around with apex:param's, but those aren't a great solution here, since our value is determined by a selectList, a part of the view state we need to submit to get the data in our controller.

So instead, you can use apex:actionRegion! When you submit the page inside an actionRegion, it only submits the data in that region- nothing else. By wrapping our selectList in an actionRegion, we can submit just that input to the controller. This gives us data to make decisions with, without triggering the validation on the page.

I've attached an example below, with a required field, and an actionRegion to get around that field.

Controller

public class SomeClass {

    public SomeClass(ApexPages.StandardController controller) {}

    public List<SelectOption> Options {
        get {
            return new List<SelectOption>{
                new SelectOption('Non-Industry', 'Non-Industry'),
                new SelectOption('Industry', 'Industry')
            };
        }
    }

    public String SelectedValue { get; set; }

    public Boolean ShowIndustry {
        get {
            if (ShowIndustry == null) {
                ShowIndustry = false; 
            }

            return ShowIndustry; 
        }
        set;
    }

    public PageReference CheckIndustry() {
        if (SelectedValue == 'Industry') {
            ShowIndustry = true; 
        } else {
            ShowIndustry = false; 
        }

        return null; 
    }

}

Visualforce

<apex:page standardController="Contact" extensions="SomeClass" >

    <apex:form>

        <apex:outputPanel id="messages">
            <apex:pageMessages></apex:pageMessages>
        </apex:outputPanel>

        <apex:inputField value="{!Contact.LastName}" required="true" />

        <apex:actionRegion >
            <apex:selectList size="1" value="{!SelectedValue}" required="true">
                <apex:selectOptions value="{!Options}" />
                <apex:actionSupport action="{!CheckIndustry}" event="onchange" reRender="SomeSection" />
            </apex:selectList>
        </apex:actionRegion>

        <apex:outputPanel id="SomeSection">
            <apex:outputPanel rendered="{!ShowIndustry}">
                <h2>
                    Industry Info
                </h2>
            </apex:outputPanel>
        </apex:outputPanel>

    </apex:form>
</apex:page>
| improve this answer | |

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.