0

I have a Visualforce page with a controlling picklist field and a dependent picklist field. Both fields are required entry during record creation, but instead of using the required parameter on apex:inputField, the controller is validating the fields and returning a pageMessage if an entry is not made.

When I select a value on the controlling picklist and leave the dependent blank and submit, I get the page message as expected, but now the dependent picklist shows no values except --None--. The controlling picklist still shows the value I selected, but the dependent has nothing for me to select, even though there should be values based on the controlling value.

The only way to get the dependent picklist values populated again is to select some other value in the controlling picklist, at which point the correct dependent values appear.

I've tried rerendering various parts of the page when the submit occurs, but nothing works. I've also omitted the rerender and that doesn't work either.

Has anyone seen this before?

I inherited this page/controller from a previous developer and there's a lot of bad code, so I have to exclude a lot of irrelevant code. The page is based on the Case standard controller and is using a controller extension.

Here is a portion of the VF page. Product_Line__c is the controlling picklist, Version__c is the dependent.

<apex:pageBlockSection id="pbs1" columns="2">
    <apex:outputLabel id="supCasePLlbl" value="{!$ObjectType.Case.Fields.Product_Line__c.label}" styleClass="requiredField" />
    <apex:outputLabel id="supCaseVlbl" value="{!$ObjectType.Case.Fields.Version__c.label}" styleClass="requiredField" />
    <apex:outputPanel >
        <apex:actionRegion >
            <apex:inputField id="supCasePLdd" value="{!caseObj.Product_Line__c}" styleClass="support-case-form-control">
                <apex:actionSupport event="onchange" action="{!checkproductline}" reRender="msgs,supCasePOS1,supCasePOS2,supCaseA1,supCaseAdd,supCaseA2,supCasePH1,supCasePH2"/>
            </apex:inputField>
        </apex:actionRegion>
    </apex:outputPanel>
    <apex:outputPanel id="supCaseVdd">
        <apex:inputField value="{!caseObj.Version__c}" styleClass="support-case-form-control left-align"/>
    </apex:outputPanel>

Here's the command button:

<apex:commandButton styleClass="btn btn-primary text-uppercase" value="Submit" action="{!submit}" />

Here's the extension's constructor:

public Case caseObj {get; set;}

public CustomerCommunityCaseController() {
    caseObj = new Case();
    init(); // does some actions that do not affect caseObj
}

Here is the method in the controller extension:

public PageReference submit() {
        // validate() checks the field entries.  Since I don't select a dependent field value,
        // it will return false.
        if (validate()) {
            // save actions occurs here, I'm omitting the code.
        }
        return null;
}
1

In my experience VF doesn't care what the value of the field on the page is. It cares about what the value of the field on the record is.

Your code doesn't provide enough information but my recomendation is on page load, create your record and make sure to use your record as a container to maintain state. Here is a minimum viable example that I was able to get to work in my org:

Page:

<apex:page controller="testPageController" doctype="html-5.0" action="{!init}">
    <apex:outputPanel id="renderTarget">
        <apex:pageMessages/>
        <apex:form>
            <apex:inputField value="{!c.control__c}"/>
            <apex:inputField value="{!c.dependant__c}"/>
            <apex:commandButton value="Save" action="{!Save}" rerender="renderTarget"/>
        </apex:form>
    </apex:outputPanel>
</apex:page>

Controller:

public class testPageController {
    public Case c {get; set;}

    public void init() {
        c = new Case();
    }

    public Void save() {
        system.debug(c.dependant__c);
        if (c.dependant__c == null) {
            apexPages.addMessage( new ApexPages.message(
                ApexPages.Severity.FATAL,
                'Error'
            ));

            system.debug('error');
        } else {
            apexPages.addMessage( new ApexPages.message(
                ApexPages.Severity.Confirm,
                'Success'
            ));

            system.debug('Success');
        }
    }
}
  • The control field has only 2 values, control 1 and control 2
  • The dependant field has only 2 values, dep 1 and dep 2.
  • control 1 only maps to dep 1 and control 2 only maps to dep 2

Minimum VIable Code samples

When you post code I strongly advise you make your code as close to copy/pasta as possible so that someone can copy your exact code into a brand new org with little modifications and reproduce your issue. This serves as an important troubleshooting step as you may reveal that the issue is not related to what you think it is. The code I provided is a good example as all you need to do is create a dependent field on case, which I know you already have.

When you strip down a page that you have and show only the parts that you think are relevant, you remove information that may be key into identifying the issue. Conversely, if you paste your entire page/controller it is unwieldy and likely will make people not want to help you. Not only are minimal viable code samples useful for troubleshooting, they get your point across quickly and allow us to easily answer your question.

Here's a sample of a question I asked with a minimum viable code sample.

  • Thanks gnerb. I inherited this page/controller and it's a big mess, I had hard time deciding what to post. I did build a basic page/controller to verify that the basic dependent field worked properly, still trying to untangle why this page is not working right. – David Cheng Oct 18 '18 at 23:15

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.