1

I have a simple page with a form that is supposed to create a custom object. This custom object has several picklist fields. I can dynamically populate the selectLists, but when I give the selectList the object's picklist value, it disappears from the page.

What I have on the page:

<apex:selectList value="{!sa.Machine__c}" multiselect="false" label="Machine" size="1">
    <apex:selectOptions value="{!machineOptions}"/>
</apex:selectList>

In my controller:

public Change__c sa { get; set; }

public Controller() {
    sa = new Change__c();
    prePopulateOptions();
}

private void prePopulateOptions() {
    machineOptions = populateOptions(Change__c.Machine__c.getDescribe());
}

private List<SelectOption> populateOptions(Schema.DescribeFieldResult fieldDescription) {
    List<SelectOption> options = new List<SelectOption>();

    // For each picklist value, create a new select option
    for (Schema.Picklistentry picklistEntry: fieldDescription.getPicklistValues()){
        options.add(new SelectOption(pickListEntry.getValue(),pickListEntry.getLabel()));
    }
    return options;
}

If I remove the selectList value, or change the value to a String object, it shows the selectList on the page. But it does not now.

Ultimately, I just want a convenient way to upsert the sa object.

2

It looks like you are simply adding the values from the field schema to selectOption without modification and showing in the page. In that case, if you use simply input field as below it will give same result,

<apex:inputField value="{!sa.Machine__c}"/>

This will not require you to set the selected value to object explicitly. If there is some other reason and you want it to be selectOption list, you can set the selected value to a string property in controller and in the save method, you can overwrite the Machine__c field value with the string property value.

  • It took me a while to figure out what you meant, but you are correct. It is easier to use the inputField directly than setting up the list myself. And it is good enough for my purposes. The reason I couldn't view the fields was (obviously) not related to all of this directly, but rather because the field-level security wasn't set to visible. – Oxidator Apr 15 '15 at 3:45

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.