1

Apex Class :

 public with sharing class AccountController{


public String getListOfAccounts { get; set; }

    public String selectedaccountId {set;get;}


  public List<SelectOption> getListOfAccounts()
{
           List<Account> AccountList = [select id,Name from Account] ;
           System.debug('Accounts'+AccountList.size());
           List<SelectOption> AccountOptionList = new List<SelectOption>();
           AccountOptionList .add(new SelectOption( ' ' ,'---Select---'));
           for(Account acc : AccountList )
           {
                      AccountOptionList .add(new SelectOption(acc.id , acc.Name));
           }
          return AccountOptionList ;
}

public AccountController()
{
                selectedaccountId ='';
}

}

VF Page :

<apex:page controller="AccountController">
<apex:form >
   <apex:pageBlock >
     <apex:pageBlockSection title="Select the Account" >
         <apex:OutputPanel >
              <apex:selectList value="{!selectedaccountId}" size="1" multiselect="false"  >
                  <apex:selectOptions value="{!getListOfAccounts}" />
               </apex:selectList>
          </apex:OutputPanel>
      </apex:pageBlockSection>
  </apex:pageBlock>
</apex:form>
</apex:page>

I want to display the entire list of Accounts in the org in the picklist of VF Page . What can be the error since I'm not getting the Accounts displayed in the VF page?

0

1 Answer 1

2

You have defined the getListOfAccounts which needs to be property only and you will bind that property with selection option in following way

<apex:selectOptions value="{!ListOfAccounts}" />

and your controller will look like this

public with sharing class AccountController {

public List<SelectOption> getListOfAccounts()
{
           List<Account> AccountList = [select id,Name from Account] ;
           System.debug('Accounts'+AccountList.size());
           List<SelectOption> AccountOptionList = new List<SelectOption>();
           AccountOptionList .add(new SelectOption( ' ' ,'---Select---'));
           for(Account acc : AccountList )
           {
                      AccountOptionList .add(new SelectOption(acc.id , acc.Name));
           }
          return AccountOptionList ;
}

    public String selectedaccountId { get; set; }

    public AccountController() {

    }
}
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .