function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
rzsrzs 

Populating a multiselect list using a SOQL query

Hello,

 

I have added a multiselect List to my VisualForce page, and i want to populate it with the list of all Account Names. My Visual Force page is as follows:

 

<apex:page controller="MyController" tabStyle="Account">
   <apex:PageBlock title="My Page Block">
  <apex:form >
     
          <apex:selectList multiselect="true">
              <apex:selectOptions value="{!availableAccountsList}"></apex:selectOptions>
          </apex:selectList>
     
  </apex:form>
   </apex:PageBlock>
</apex:page>

 My Controller contains the following code :

 

 

public class MyController {

   public List <Account> availableAccountsList() {
       return [Select Name from Account];
   }

}

 

But,im getting the Error 'Error: Unknown property 'MyController.availableAccountsList'

 

 

Any way to fix this problem ?

 

Please help.

Thank You.

 

JimRaeJimRae

You either need to make the availableAccountList a property, or at least make the method a get method.

 

public class MyController {

   public List <Account> getavailableAccountsList() {
       return [Select Name from Account];
   }

}

 

Ispita_NavatarIspita_Navatar

Please take a look at the enlcosed code hope this will help you:-

Visual Force Page

<apex:page controller="MyController" tabStyle="Account">
   <apex:PageBlock title="My Page Block">
  <apex:form >
     
          <apex:selectList value="{!names}" multiselect="true">
            <apex:selectOptions value="{!items}"/>
            </apex:selectList>

    

  </apex:form>
   </apex:PageBlock>
</apex:page>

 

Controller Code

 

public 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;
          
   }
}

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved.

 

 

sivassivas

Your return type should be of type selectOption try doing some thing like this with your controller

 

 

public list<selectOption> availableAccountsList(){
// Your logic
return availableAccountsList;
}

 

 

Zoom_VZoom_V

Ispita,

 

I just came by this code you put together and it's really awesome. I am right now trying to do something very similar to this and I was wondering\hoping you could help me with it.

 

Basically, I'm trying to do what your code does except I would like to get a list from a custom object (called PartnerProfile_c) into another custom object (called Contract_c). 

 

Each Partner has its own profile entry which is a child to an Account.. I'm essentially trying to create new contracts for Accounts in which I can choose which Partners of that Account will be included in the contract. So, I'd like to populate multi-select list in the Contract with all of the Partner_c entries whose Account Name is the same as the Account Name on the Contract.

 

I hope that made sense. It's very similar to what you are doing here, except I am trying to run a more precise SOQL query. 

 

If anyone can help I would be extremely grateful. 

 

Thank you in advance.