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:pageBlock>
</apex:page>
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++)
{
if(i<10)
{
names[i]=availableAccountsList[i].Name;
}
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.