You need to sign in to do that
Don't have an account?
aurelioc
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;}
}
}
If you know the name of the field you are trying to get the values for then you can use this....
and call if from your code like this:
All Answers
If you know the name of the field you are trying to get the values for then you can use this....
and call if from your code like this:
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.
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?