0

I have a Visualforce form with the following pageBlockSection:

        <apex:pageBlockSection title="Select Vehicle">
            <apex:selectList id="manufacturer" label="Manufacturer" value="{!selectedManufacturer}" size="1">
                <apex:selectOptions value="{!manufacturers}"/>
                <apex:actionSupport event="onchange" reRender="model"/>
            </apex:selectList>
            <apex:selectList id="model" label="Model" size="1">
                <apex:selectOptions value="{!models}"/>
            </apex:selectList>                
        </apex:pageBlockSection>

Code in the controller extension looks like this.

Public String selectedManufacturer {
    get {
        System.debug('Getting selectedManufacturer property value!');
        return selectedManufacturer;
    }
    set {
        // THIS BLOCK IS NOT GETTING EXECUTED!!!!
        System.debug('**Setting** selectedManufacturer property value!');
        selectedManufacturer = value;
    }
}

// Populate the manufacturer picklist.
public List<SelectOption> getManufacturers() {
    List<SelectOption> manufOptions;
    manufOptions = new List<SelectOption>();
    System.debug('Hit the getManufacturers code!');
    manufOptions.add(new SelectOption('BMW', 'BMW'));
    manufOptions.add(new SelectOption('Jaguar', 'Jaguar'));
    manufOptions.add(new SelectOption('Mercedes', 'Mercedes'));
    return manufOptions;
}

// Populate the model picklist.
public List<SelectOption> getModels() {
    List<SelectOption> modelOptions;
    modelOptions = new List<SelectOption>();
    System.debug('Hit the getModels code!');
    System.debug('selectedManufacturer set to: ' + selectedManufacturer);
    if(selectedManufacturer == 'BMW') {
        modelOptions.add(new SelectOption('3 Series', '3 Series'));
        modelOptions.add(new SelectOption('5 Series', '5 Series'));
        modelOptions.add(new SelectOption('7 Series', '7 Series'));
    }
    else if(selectedManufacturer == 'Jaguar') {
        modelOptions.add(new SelectOption('XF', 'XF'));
        modelOptions.add(new SelectOption('XK', 'XK'));
    }
    else if(selectedManufacturer == 'Mercedes') {
        modelOptions.add(new SelectOption('B-Class', 'B-Class'));
        modelOptions.add(new SelectOption('C-Class', 'C-Class'));
        modelOptions.add(new SelectOption('E-Class', 'E-Class'));
    }
    else if(selectedManufacturer == null) {
        modelOptions.add(new SelectOption('AAARGH!', 'AAARGH!'));
    }
    return modelOptions;
}

I have debugged fairly extensively and the root of the problem seems to be the selectedManufacturer property set accessor is not being executed in the controller extension.

Can anybody see what the problem might be? I have been working on this for nine hours now, and I'm starting to go round in circles.

  • Is your selectList manufacturers getting populated? – EricSSH Jun 28 '17 at 21:45
  • @EricSSH Yes, that list is populated fine. The picklist shows BMW, Jaguar and Mercedes as expected. – Brian Jun 28 '17 at 21:53
  • I don't believe you need to do this selectedManufacturer = value; I think you might be emptying because value is null – EricSSH Jun 28 '17 at 21:56
  • @EricSSH I just tried commenting out that line and it hasn't made a difference. That property set accessor block still isn't being executed. – Brian Jun 28 '17 at 22:06
  • @Brian The posted code is just fine. I think somehow you are making selectedManufacturer null or set to something else outside of this code. – Saroj Bera Jun 28 '17 at 23:19
0

I found the cause of this problem by stripping out all the other elements on the Visualforce page. It worked perfectly with only the picklists on the page, so I started adding back the other elements one-by-one, and retesting each time.

Everything worked fine until I came to add a mandatory field back to the Visualforce page, then the property set accessor immediately stopped working again. It seems this accessor is only getting fired once all mandatory fields on the form have entries in them.

This is pretty strange behaviour and less than ideal because it forces users to complete all mandatory fields before they can use these picklists, but at least the original problem is now solved.

I hope this answer saves somebody else several hours of pain!

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.