+ Start a Discussion
NJDeveloper019NJDeveloper019 

Retrieving Picklist values for VisualForce Page

I have a single-select picklist built in a custom object holding states in my SalesForce instance.  I would like to repricate these picklist values in another picklist that I have in a custom VisualForce page.  I could enter all the values in manually with .add but this seems like a poor choice.  If someone changes values in the picklist in my custom object, I would also like the list to change in my VisualForce page.  This way a change in my custom object doesn't require a change in a VisualForce page everytime.  My question is how do I select all of the values out of the picklist in my custom object so I may bind them to my picklist in my VisualForce page.  If I have to loop through the values, that is fine too.
greenstorkgreenstork

You can instantiate your sObject in your custom controller (or extension, not sure what you're using), and then reference the actual custom object field in your visualforce page, rather than building the selectOptions list by hand. Visualforce allows to bind to an sObject. Here is some pseudo-code for your controller/extension, describing what I am talking about:

 

 

public Custom_Object__c anyOldVariable {get {return anyOldVariable;} set {anyOldVariable = value;} }

 

//And then in the constructor, instantiate your variable

public My_Controller () {

 

anyOldVariable = new Custom_Object__c();

 

}

 

 And then from the Visualforce page, you can use your actual picklist field from the custom object:

 

 

<apex:inputField id="My Custom Field" value="{!anyOldVariable.My_Custom_Field__c}">

 


 

 

Message Edited by greenstork on 06-10-2009 08:57 PM
hisrinuhisrinu
public List<SelectOption> getBUOptions() 
    {
        List<SelectOption> options = new List<SelectOption>();
        options.add(new SelectOption('None','--None--'));
        Schema.DescribeFieldResult fieldResult = Account.Type.getDescribe();
        List<Schema.PicklistEntry> ple = fieldResult.getPicklistValues();
        for(Schema.PicklistEntry p : ple)
        options.add(new SelectOption(p.getValue(), p.getValue()));        
        return options;
    }
 
You can get all the picklist values using the above describe call.
Message Edited by hisrinu on 06-11-2009 04:10 AM
ccgccg

How can I use this in a trigger?

 

trigger NewSpecialtyValue on Lead (before update, before insert) {
    //Goal: Set the variable ValueDoesNotExist_c to True if an imported value for the pickstlist "Specialty" does not exist
   
   //Define and use the variable ValueDoesNotExist
   
   
   
    //Create an array to store all of the existing "Specialty" values from a Lead picklist "Specialty"
        //List <String> LPV = Lead.Specialty_Description__c.getDescribe();
  


      List<SelectOption> getBUoptions()
    {
        List<SelectOption> options = new List<SelectOption>();
        options.add(new SelectOption('None','--None--'));
        //Schema.DescribeFieldResult fieldResult = Account.Type.getDescribe();
        Schema.DescribeFieldResult fieldResult = Lead.LAMMICO_Specialty_Description__c.getDescribe();
        List<Schema.PicklistEntry> ple = fieldResult.getPicklistValues();
        for(Schema.PicklistEntry p : ple)
        options.add(new SelectOption(p.getValue(), p.getValue()));        
        return options;
    }    
   Lead testlead = Trigger.new[0];
   testlead.Description = 'getBUOptions';
 
    }
    //See if the "Specialty" value for this Lead exists in that array.
    
    //If value does not exist, then set ValueDoesNotExist to True
    //If value does exist, set ValueDoesNotExist to False