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
Ravindra reddy MRavindra reddy M 

How to store query result values in picklist

Hi All,
I query Account records first, then i need to store these values in pick list, 
public with sharing class ProjectNameListed {
public string projectName{get;set;}
  public  List<Account>   ProjectName(){
        list<selectOption> projectDetails=new list<selectOption>();
         List<Account>  pro=[select id,name from Account];
         return pro ;
        }       
}

vf page:

<apex:page controller="ProjectNameListed">
    <apex:form >
        <apex:pageBlock >
            <apex:pageBlockSection >
                <apex:outputLabel value="Project Name :"/>
                  
                <apex:SelectList size="1"/>
                <apex:selectOptions value="{!pro}"/>
             </apex:pageBlockSection>         
        </apex:pageBlock>
    </apex:form>
</apex:page>

please let me know the solution

Thanks & Regards
Raveendra
Shruti SShruti S

I think there are couple of errors in your code. Can you please try the below code - 

Apex Controller

public with sharing class ProjectNameListed {
    public List<SelectOption> projectNames {get;set;}
    
    public ProjectNameListed() {
        getProjectNames();
    }
    
    public void getProjectNames(){
        projectNames = new List<SelectOption>();
        
        List<Account> projects = [
            SELECT  Id
                    ,Name
            FROM    Account
        ];
        
        for( Account acc : projects ) {
            projectNames.add( new SelectOption( acc.Id, acc.Name ) );
        }
    }
}

'ProjectNameListed' class has a method  'getProjectNames' which queries for all the available Accounts, iterates them to create options and adds to a get set variable of type 'List<SelectOption>'. The method 'getProjectNames' is then invoked via the constructor. It is very important to note that the code written in the constructor is the first thing that runs when a page loads.

Visualforce

<apex:page controller="ProjectNameListed">
    <apex:form >
        <apex:pageBlock >
            <apex:pageBlockSection >
                <apex:outputLabel value="Project Name :"/>
                  
                <apex:selectList size="1">
                    <apex:selectOptions value="{!projectNames}"/>
                </apex:selectList>
             </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>
Note that the <apex:selectList> is not a self closing tag and the <apex:selectOptions> tag is the child of <apex:selectList> tag.
Ravindra reddy MRavindra reddy M
Hi Shruti S,
Now its working fine, I am new to salesforce please let me know the best sites to practice coding.

Thanks & Regards
Raveendra
Shruti SShruti S
From my point of view, the best resource to learn Salesforce these days is via Trailhead.