0

I am trying to display a picklist based on values of a soql query in my controller. However the picklist is not working- it is not showing any values. What am I doing wrong here?

Vf page snippet:

<apex:outputLabel value="VENDOR Name :"/>
    
<apex:selectList size="1">
    <apex:selectOptions value="{!vendorAccountNames}"/>
</apex:selectList><br/><br/>  

Controller snippet:

public List<SelectOption> vendorAccountNames {get;set;}
recordtype rt = [select id from recordtype where name = 'Vendor'];
public void getProjectNames(){
    vendorAccountNames = new List<SelectOption>();
    List<Account> projects = [
        SELECT  Id,Name FROM Account where recordtypeid = :rt.id
    ];
    
    for( Account acc : projects ) {
        vendorAccountNames.add( new SelectOption( acc.Id, acc.Name ) );
    }
}
1
  • 2
    Did you actually call getProjectNames() somewhere in your controller? If not, then there'd be no values to render. – sfdcfox Apr 15 at 20:15
0

Rather than having to directly call the method through the controller like sfdcfox mentioned in his response, you could change the method from void to List<SelectOption>, rename to getVendorAccountNames, declare vendorAccountNames inside the method itself instead of outside, and return vendorAccountNames in order to take advantage of Visualforce Getter Methods.

Controller:

public List<SelectOption> getVendorAccountNames() {
    List<SelectOption> vendorAccountNames = new List<SelectOption>();
    for (Account acc : [SELECT  Id, Name FROM Account WHERE RecordType.Name = 'Vendor']) {
        vendorAccountNames.add(new SelectOption(acc.Id, acc.Name));
    }
    return vendorAccountNames;
}

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.