+ Start a Discussion
sanjeev342sanjeev342 

How do I display picklists?

I'm trying to display picklists on my custom VF page. These picklists, Prefix__c and Suffix__c are both custom fields in the Contact object. I am using the following code, but I keep getting the following error:

 

Method does not exist or incorrect signature: [String].getDescribe();

 


This example has been used by many people and they seem to get it working. I'm not sure what's happening here. Here's my code:

 

    public List<SelectOption> getCountries()
    {
        List<SelectOption> options = new List<SelectOption>();
        
        Schema.DescribeFieldResult fieldResult =
            Contact.Prefix__c.getDescribe();
        List<Schema.PicklistEntry> ple = fieldResult.getPicklistValues();
        
        for( Schema.PicklistEntry f : ple)
        {
            options.add(new SelectOption(f.getLabel(), f.getValue()));
        }       
        return options;
    }

 

Thanks,
Sanjeev

HariniHarini

Hi,

 

Do this.. I haven't tested this, but this should work. I have just outlined the logic .Let me know if you need more help on this.

 

Schema.DescribeSObjectResult describeCont = Schema.SObjectType.Contact;

Field[] fields=describeCont.getFields();

for (int i = 0; i < fields.length; i++) {
          Field field = fields[i];

// If this is a picklist field, show the picklist values
          if (field.getType().equals(FieldType.picklist)) {
              PicklistEntry[] picklistValues =
                  field.getPicklistValues();
              if (picklistValues != null) {

 for (int j = 0; j < picklistValues.length; j++) {
                  if (picklistValues[j].getLabel() != null) {
                                          picklistValues[j].getLabel()
                    );
                  }
                } 

Yoganand GadekarYoganand Gadekar

If you want to display picklist on VF page and that picklist is a field on object then you can simply use inputfield attribute.

Following is the example that displays [picklist rating field of account object

 

<apex:page standardController="Account">
    <apex:form >
        <apex:pageBlock >
             <apex:inputField value="{!account.Rating}"/>
        </apex:pageBlock>
    </apex:form>
</apex:page>

 

Hope tgis helps,

Mark this as answer and hit kudos if it heklps you in right direction.

Karthikeyan JayabalKarthikeyan Jayabal

You're using Schema.DescribeFieldResult variable type instead of Schema.DescribeSObjectResult. That's the reason for the error.

 

You can also directly fetch the PicklistEntry list, this way:

List<Schema.PicklistEntry> ples = Schema.sObjectType.Contact.fields.Prefix__c.getPicklistValues();