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
Suman MSuman M 

How to Pass Custom Picklist Field Values into a selectList in Visualforce Page

Hi,

 

I have created a Custom Picklist Field ( Country__c ) in a Standard Object ( Contact ). I have created a VF page and have a selectList component in it, I want to pass the Picklist filed values into VF page. Please help me on this.

 

VF Page:

 

<apex:page standardController="Contact" extensions="countryExtension" standardStylesheets="true">

<apex:form >
<apex:pageBlock title="Contact Edit" mode="edit">
<apex:pageBlockButtons >
<apex:commandButton action="{!save}" value=" Save "> </apex:commandButton>
<apex:commandButton action="{!cancel}" value="Cancel"></apex:commandButton>
</apex:pageBlockButtons>
<apex:pageBlockSection title="General Information" columns="2">
<apex:inputField value="{!Contact.FirstName}"></apex:inputField>
<apex:inputField value="{!Contact.LastName}"></apex:inputField>
<apex:inputField value="{!Contact.Department}"></apex:inputField>
<apex:inputField value="{!Contact.Phone}"></apex:inputField>

<apex:inputField value="{!Contact.Email}"></apex:inputField>

</apex:pageBlockSection>
<apex:pageBlockSection columns="1" showHeader="false">
<apex:pageBlockSectionItem >
<apex:outputLabel value="Country Name" for="accts"></apex:outputLabel>
<apex:selectList id="accts" value="{!Contact.Country__c}" size="1" title="Contact">
<apex:selectOptions value="{!accts}"></apex:selectOptions>
</apex:selectList>
</apex:pageBlockSectionItem>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>

 

Controller:

 

public class countryExtension
{
public Contact c;

public countryExtension(ApexPages.StandardController stdController)
{
this.c = (Contact)stdController.getRecord();
}
public List<selectOption> getaccts()
{
List<selectOption> options = new List<selectOption>();
options.add(new selectOption('', '- None -'));
for (Contact c : [SELECT id, Country__c FROM Contact Limit 1])
{
System.debug(c);
options.add(new selectOption(c.id, c.Country__c));

}
return options;
}
}

Best Answer chosen by Admin (Salesforce Developers) 
Suman MSuman M

Hi Harsha, 

 

Thank You very much for the help.. Now it is working!!.

 

Yeah I can use simple component <apex:inputField value="{!Contact.Country__c}"/> to get the picklist into vf page. But I'm trying to make use of selectList, so that i can learn using it in other requirments.

 

 

 

 

All Answers

kiranmutturukiranmutturu

you need to use like below 

 

public List<SelectOption> Options { get;set{

Schema.DescribeFieldResult countryFieldDescription = contact.Country__c.getDescribe();
statusOptions = new list<SelectOption>();

for (Schema.Picklistentry picklistEntry : countryFieldDescription.getPicklistValues())
{
Options.add(new SelectOption(pickListEntry.getValue(),pickListEntry.getLabel()));
}

}

}

Suman MSuman M

Hi Kiran,

 

Thanks for the reply,

 

I'm sry, I'm not clear about where to place the code in my controller, I have tried as below. but getting an error. "expecting a semi-colon, found '{' at line 11 column 27" Could you please help on this. Thank You.

 

public class countryExtension
{
public Contact c;

public countryExtension(ApexPages.StandardController stdController)
{
this.c = (Contact)stdController.getRecord();
}
public List<selectOption> getaccts()
{
List<SelectOption> Options {get;set{
Schema.DescribeFieldResult countryFieldDescription = contact.Country__c.getDescribe();
selectOptions = new list<SelectOption>();

for (Schema.Picklistentry picklistEntry : countryFieldDescription.getPicklistValues())
{
Options.add(new SelectOption(pickListEntry.getValue(),pickListEntry.getLabel()));
}
}
}
return options;
}
}

harsha__charsha__c

 Hi Suman,

 

Your logic is entirely right.

 

Did you check whether Contact records are getting inserted or not..?

 

I guess that the Contact records are getting inserted...look into that once

 

And one more thing is that remove the limit from the query and use order by keyword, by which you can get the list in an order and will be easier to cross-checking

Suman MSuman M

Hi Harsha,

I'm getting an error when I'm trying the below code. It is aying ' System.NullPointerException: Argument 2 cannot be null ' in 'Class.countryPicklistExtension.getconts: line 16, column 1 '

 

If you don't mind could you please try this in your Developer instance. Please just create a PickList Fielld ( Country ) with some values in your Contact Object and please try this code.

 

Please help me on this. Thank You.


VF Page
**************************************************************************************************

<apex:page standardController="Contact" standardStylesheets="true" extensions="countryPicklistExtension">
<apex:form >
<apex:pageBlock title="Contact Edit" mode="edit">
<apex:pageBlockButtons location="bottom">
<apex:commandButton action="{!save}" value="Save"/>
<apex:commandButton action="{!Cancel}" value="Cancel"/>
</apex:pageBlockButtons>
<apex:pageBlockSection title="Contact Details" columns="2" collapsible="false">
<apex:inputField value="{!Contact.FirstName}"/>
<apex:inputField value="{!Contact.LastName}"/>
<apex:inputField value="{!Contact.Department}"/>
<apex:inputField value="{!Contact.Phone}"/>
<apex:inputField value="{!Contact.Email}"/>
</apex:pageBlockSection>
<apex:pageBlockSection columns="1" showHeader="false">
<apex:pageBlockSectionItem >
<apex:outputLabel value="Country Name" for="conts"></apex:outputLabel>
</apex:pageBlockSectionItem>
<apex:selectList id="conts" value="{!Contact.Country__c}" size="1">
<apex:selectOption value="{!conts}">
</apex:selectOption>
</apex:selectList>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>

 

********************************************************************************************
Controller
***********************************************************

 

public class countryPicklistExtension
{
public Contact c;

public countryPicklistExtension(ApexPages.StandardController stdController)
{
this.c = (Contact)stdController.getRecord();
}
public List<selectOption> getconts()
{
List<selectOption> options = new List<selectOption>();
options.add(new selectOption('', '- None -'));
for (Contact c : [SELECT id, Country__c FROM Contact Limit 1])
{
System.debug(c);
options.add(new selectOption(c.id, c.Country__c));
}
return options;
}

}

 

********************************************************************

harsha__charsha__c

Hi Suman,

 

The reason for this is pretty clear that the record which you are querying here is having the Country field as null..

 

That's why you are getting this..!

 

 

 

Anyhow, let me try this and share the result here..! 

Suman MSuman M
Hi Harsh, Picklist Filed ( Country ) in the object is not null, I have values in the picklist. But still it is showing that error.. Please check that. Thank you so much for your help.. I'll wait for your reply. *Regards:** Suman M** *
harsha__charsha__c

Hi Suman,

 

Now I'm clear with your requirement..

 

You can just use the following line

 

<apex:inputField value="{!Contact.Country__c}"/>  to get the Country__c values

 

 

 

 

 

 

 

 

 

Suman MSuman M

Hi Harsha, 

 

Thank You very much for the help.. Now it is working!!.

 

Yeah I can use simple component <apex:inputField value="{!Contact.Country__c}"/> to get the picklist into vf page. But I'm trying to make use of selectList, so that i can learn using it in other requirments.

 

 

 

 

This was selected as the best answer