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
Suresh RaghuramSuresh Raghuram 

Populate picklist from the custom object to the Visualforce page

Hi

 

I am new to the Visualforce

 

I want to populate a picklist called Security_Question__C which is a field on the MyContact__c custom object.

 

 

I tried different way , but it is not working could any body suggest me a solutution with code.

 

Best Answer chosen by Admin (Salesforce Developers) 
Naidu PothiniNaidu Pothini
<apex:page controller="SinglepicklistforMyAccount" action="{!autoRun}">
    <apex:form >
        <apex:pageblock>
            <apex:pageBlockSection id="InfoId" columns="1" >
                <apex:outputLabel value="New Account Status" />
                <apex:selectList size="1" value="{!SelectedValue}">
                    <apex:selectOptions value="{!statusOptions}"/>
                    <apex:actionSupport event="onchange" action="{!checkValue}" />
                </apex:selectList>
            </apex:pageBlockSection>
        </apex:pageblock>
    </apex:form>
</apex:page>

 

public class SinglepicklistforMyAccount 
{
    public string selectedValue { get;set; }
    public List<SelectOption> statusOptions { get;set; }
    
    public void autoRun()
    {
        Schema.DescribeFieldResult statusFieldDescription = Account.Rating.getDescribe();
        statusOptions = new list<SelectOption>();
        
        for (Schema.Picklistentry picklistEntry : statusFieldDescription.getPicklistValues())
        {
            statusOptions.add(new SelectOption(pickListEntry.getValue(),pickListEntry.getLabel()));
        }
    }

    public void checkValue()
    {
        System.debug('----------------'+selectedValue);
    }
}

 

 

check the value in the debug log. Let me know if this doesnt work.

All Answers

Naidu PothiniNaidu Pothini
<apex:selectList size="1" value="{!selectedValue}" >
	<apex:selectOptions value="{!statusOptions}" />
</apex:selectList> 

 

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

Schema.DescribeFieldResult statusFieldDescription = MyContact__c.Security_Question__C.getDescribe();
                    
for (Schema.Picklistentry picklistEntry : statusFieldDescription.getPicklistValues())
{
	statusOptions.add(new SelectOption(pickListEntry.getValue(),pickListEntry.getLabel()));
}

 

Try this. Let me know if it doesnt work.

Suresh RaghuramSuresh Raghuram

<apex:page controller="SinglepicklistforMyContact" >
<apex:form >
<apex:selectList size="1" value="{!SelectValue}"></apex:selectList>
<apex:selectOption value="{!statusOptions}"></apex:selectOption>
</apex:form>
</apex:page>

 

In place of {!SlectValue} i used MyContact__c.Security_Question__c 

 

but it is showing the error as invalid identifier MyContact__C

Naidu PothiniNaidu Pothini
<apex:page controller="SinglepicklistforMyContact" >
    <apex:form >
        <apex:selectList size="1" value="{!SelectValue}">
            <apex:selectOptions value="{!statusOptions}"/>
        </apex:selectList>
    </apex:form>
</apex:page>

 

    public string selectValue { get;set; }
    public List<SelectOption> statusOptions { get;set; }
    
    public void autoRun()
    {
        Schema.DescribeFieldResult statusFieldDescription = MyContact__c.Security_Question__c.getDescribe();

        statusOptions = new list<SelectOption>();
        
        for (Schema.Picklistentry picklistEntry : statusFieldDescription.getPicklistValues())
        {
            statusOptions.add(new SelectOption(pickListEntry.getValue(),pickListEntry.getLabel()));
        }
    }

 

 

 

try this.

Suresh RaghuramSuresh Raghuram

It is not giving any errors but  result is empty picklist

Naidu PothiniNaidu Pothini
<apex:page controller="SinglepicklistforMyAccount" action="{!autoRun}">
    <apex:form >
        <apex:pageblock>
            <apex:pageBlockSection id="InfoId" columns="1" >
                <apex:outputLabel value="New Account Status" />
                <apex:selectList size="1" value="{!SelectedValue}">
                    <apex:selectOptions value="{!statusOptions}"/>
                    <apex:actionSupport event="onchange" action="{!checkValue}" />
                </apex:selectList>
            </apex:pageBlockSection>
        </apex:pageblock>
    </apex:form>
</apex:page>

 

public class SinglepicklistforMyAccount 
{
    public string selectedValue { get;set; }
    public List<SelectOption> statusOptions { get;set; }
    
    public void autoRun()
    {
        Schema.DescribeFieldResult statusFieldDescription = Account.Rating.getDescribe();
        statusOptions = new list<SelectOption>();
        
        for (Schema.Picklistentry picklistEntry : statusFieldDescription.getPicklistValues())
        {
            statusOptions.add(new SelectOption(pickListEntry.getValue(),pickListEntry.getLabel()));
        }
    }

    public void checkValue()
    {
        System.debug('----------------'+selectedValue);
    }
}

 

 

check the value in the debug log. Let me know if this doesnt work.

This was selected as the best answer
Suresh RaghuramSuresh Raghuram

Thank you Naidu it worked and could you suggest some programs to learn visual force and apex in , like some examples and scenarios for triggers and vf pages from your real time scenarios.

 

Once again thank you very much for the help.

Ricardo DiazRicardo Diaz
@Suree You need to include 
action="{!autoRun}"

On the page where you call the controller:
 
<apex:page controller="SinglepicklistforMyAccount" action="{!autoRun}">

Thank you for this!