Creating multi-select picklist\selectoption list through query

I am attempting to create a multi-select picklist whose options will come from a query which is shot out upon the selection of another field. I then want to put those selections into a field. It's very similar to dependant list type of function, but it is proving to be much more complicated than I thought it would be.

my scenario : I have accounts which have sub-accounts (Sub_Account__c). So, when I am creating a new Contract I would like to be able to select the Account, and then have a select list\picklist come up whose option would be the sub-accounts of that selected Account.

So far, I have been just trying to get a select list to come up with any kind of queried list just to get somewhere on it, so I am querying all Accounts. That's not really what I want to do, but I just wanted to produce some kind of list on the fly. Here is that code :

Visual Force page :

<apex:page controller="MyController">
    <apex:pageBlock title="Retrieving Query String Parameters">
        You are viewing the {!account.name} account.

Apex :

public with sharing class MyController {

    public String getItems { get; set; }

    public List<Account> availableAccountsList { get; set; }
    public String[] names = new String[10];

public MyController()

public String[] getnames() {
return names;
public void setnames(String[] names) {
this.names = names;
public  List<SelectOption> getItems() {
         availableAccountsList = [Select Name from Account];
         List<SelectOption> options = new List<SelectOption>();
         for(integer i=0;i<availableAccountsList.size();i++)
         options.add(new SelectOption(availableAccountsList[i].Name,availableAccountsList[i].Name));

         return options;


Again, this isn't really what I want to use as options. I don't want all of my Accounts. I just wanted to see if I could build a select list on the fly. Somehow I need to put a query into that which will use the selected Account as a key to pull all of the Sub-Accounts.

Any kind of help somebody can give me would be greatly appreciated.

Thank you very much.

1 Answer 1


Visual Force Page

<apex:page standardController="Contract" extensions="newContractController" >
<apex:form >
    <apex:inputfield value="{!myContract.AccountId}">
        <apex:actionSupport event="onchange" action="{!accountChanged}" />

    <apex:selectList value="{!selectedSubAccounts}" >
        <apex:selectOptions value="{!subAccountOptions}" />        



public class newContractController {

    public List<SelectOption> subAccountOptions { get; set; }
    public string [] selectedSubAccounts { get; set; }

    public Id accountId { get; set; }
    public Contract myContract { get; set; }

    public newContractController(ApexPages.StandardController controller) {
        myContract =  new Contract();

    public void accountChanged() {

        Account [] accounts = [SELECT Name, Id FROM Account WHERE ParentId = :myContract.AccountId];

        List<SelectOption> options = new List<SelectOption>();

        for (Account a : accounts) {
            options.add(new SelectOption(a.Id,a.Name));        

        subAccountOptions = options;



Here's something that may help. Accounts already have a field called Parent Account which will allow for a hierarchy of Accounts. You may want to use that instead of creating a custom Sub Account.

In this page when you pick an Account it will cause an event which will query for all Accounts that whose parent is the one you just picked

  • Outstanding Chris ! Thank you so much !!! btw - I think for now we may continue to use the custom Sub-Account object because there are a bunch of other design issues which warrant a separate object. However, it is something for me to consider. I'll have to think it out a little further. Thanks again !
    – user2428
    Mar 6, 2013 at 20:42

You must log in to answer this question.

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