0 votes
1 view
by

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 Answer

0 votes
by
 
Best answer

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;
}
Welcome to Memory Exceeded, where you can ask questions and receive answers from other members of the community.
...