+ Start a Discussion
aureliocaurelioc 

Best Way to Get All Picklists and Values for an Object

Below is a class to get all picklists and their values for an object, but I feel like there must be a more efficient way to do it. The main question is, given the name of the object as a string property, do I have to create list<Schema.SObjectType> gd = Schema.getGlobalDescribe().Values() and then loop through all items in the list until I get to the object I want? Or is there a better way to do go directly to the one I want?

--------------------


public class GetPicklistsForObject {

  public string theObjectType {get; set;} //the object we're going to get the picklists for
    
  public List<cObjects> getMyPicklist() {
    
    list<Schema.SObjectType> gd = Schema.getGlobalDescribe().Values(); 
    list<cObjects> myobjects = new list<cObjects>();  

    for(Schema.SObjectType f : gd) { //for all objects in the schema

       if (f.getDescribe().getLabel() == theObjectType) { //we've found the one we're looking for
         
            Map<String, Schema.SObjectField> theFieldmap = f.getDescribe().fields.getMap(); //field map for object
            
            for(String field: theFieldmap.keyset()){ //for each field in the object
              
                   Schema.DisplayType theResult = theFieldmap.get(field).getDescribe().GetType(); //get field type

                   if (theResult == Schema.DisplayType.Picklist){ //if it's a picklist
                     
                          //fill list with picklist values
                           list<Schema.PicklistEntry> pick_list_values = theFieldmap.get(field).getDescribe().getPickListValues();
                            
                   for (Schema.PicklistEntry a : pick_list_values) {  
                        
                        //for each value, add item to myobjects list
                        cObjects tempObject = new cObjects();
                        tempObject.myObjectName = f.getDescribe().getLabel();
                    tempObject.myFieldName = theFieldmap.get(field).GetDescribe().Name;
                    tempObject.myFieldValue = a.getValue();
                    myobjects.add(tempObject);

                   }
                   } //if it's a picklist

            } //for loop
       } //if it matches theObjectType
    }
    return myobjects;
    
  }
  
  public class cObjects {
     public String myObjectName {get; set;}
      public String myFieldName {get; set;}
      public String myFieldValue {get; set;} 
  }

}
Best Answer chosen by Admin (Salesforce Developers) 
Starz26Starz26

If you know the name of the field you are trying to get the values for then you can use this....

 

public List<selectOption> getPickValues(Sobject object_name, String field_name, String first_val) {
      List<selectOption> options = new List<selectOption>(); //new list for holding all of the picklist options
      if (first_val != null) { //if there is a first value being provided
         options.add(new selectOption(first_val, first_val)); //add the first option
      }
      Schema.sObjectType sobject_type = object_name.getSObjectType(); //grab the sobject that was passed
      Schema.DescribeSObjectResult sobject_describe = sobject_type.getDescribe(); //describe the sobject
      Map<String, Schema.SObjectField> field_map = sobject_describe.fields.getMap(); //get a map of fields for the passed sobject
      List<Schema.PicklistEntry> pick_list_values = field_map.get(field_name).getDescribe().getPickListValues(); //grab the list of picklist values for the passed field on the sobject
      for (Schema.PicklistEntry a : pick_list_values) { //for all values in the picklist list
                  
            options.add(new selectOption(a.getValue(), a.getLabel())); //add the value and label to our final list
      }
      return options; //return the List
}

 

and call if from your code like this:

 

public List<SelectOption> getYOURMETHODNAME(){ return getPickValues(YOUROBJECTNAME, 'YOURPICKLISTFIELDNAME', NULL)); }

 

 

 

All Answers

Starz26Starz26

If you know the name of the field you are trying to get the values for then you can use this....

 

public List<selectOption> getPickValues(Sobject object_name, String field_name, String first_val) {
      List<selectOption> options = new List<selectOption>(); //new list for holding all of the picklist options
      if (first_val != null) { //if there is a first value being provided
         options.add(new selectOption(first_val, first_val)); //add the first option
      }
      Schema.sObjectType sobject_type = object_name.getSObjectType(); //grab the sobject that was passed
      Schema.DescribeSObjectResult sobject_describe = sobject_type.getDescribe(); //describe the sobject
      Map<String, Schema.SObjectField> field_map = sobject_describe.fields.getMap(); //get a map of fields for the passed sobject
      List<Schema.PicklistEntry> pick_list_values = field_map.get(field_name).getDescribe().getPickListValues(); //grab the list of picklist values for the passed field on the sobject
      for (Schema.PicklistEntry a : pick_list_values) { //for all values in the picklist list
                  
            options.add(new selectOption(a.getValue(), a.getLabel())); //add the value and label to our final list
      }
      return options; //return the List
}

 

and call if from your code like this:

 

public List<SelectOption> getYOURMETHODNAME(){ return getPickValues(YOUROBJECTNAME, 'YOURPICKLISTFIELDNAME', NULL)); }

 

 

 

This was selected as the best answer
aureliocaurelioc

Thank you for your response.  I should have been a bit more clear: I don't know in advance what the picklist names are - I want to find all picklists for the object and be able to list them and their values.

 

But you answered the question with the following line. I should pass object_name as an sObject, then I can do the following and I won't have to loop through all objects to find the one I want. 

 

Schema.sObjectType sobject_type = object_name.getSObjectType(); //grab the sobject that was passed
bryan.revelant1.36697709739127bryan.revelant1.36697709739127

When the user selects the value in the pick list, would it populate the associated values to the value in text boxes in the visual force page?