I have two variables:
public String selectedObject {get; set;} //selectlist
public String selectedField {get; set;} //selectlist
public String selectedDataType {get; set;} //text
They are the selected values of my two dynamic picklist for selecting an object and then selecting a specific field and a text field for identifying its data type. I know that I can get the picklist values of a specific field a code like this:
public List<SelectOption> getPicklistValues() {
List<SelectOption> options = new List<SelectOption>();
if (selectedDataType=='PICKLIST'){
Schema.DescribeFieldResult fieldResult = Account.Type.getDescribe();
List<Schema.PicklistEntry> ple = fieldResult.getPicklistValues();
options.add(new SelectOption('','--None--'));
for( Schema.PicklistEntry f : ple) {
options.add(new SelectOption(f.getLabel(), f.getValue()));
}
}
return options;
}
It can retrieve all the picklist values of the Type field of Account object.
The problem is it is not dynamic (I wanted to replace the Account with the selectedObject value and the Type with the selectedField value and as long as the field type is PICKLIST.
How do I achieve this?
Best Answer
Instead of Account.Type (a specific field), you can use:
At that point, you can get the picklist entries as normal. It's also possible from the field's describe to see if it's a valid picklist; simply call .getType() and see if it's a DisplayType.Picklist, DisplayType.MultiPicklist, or DisplayType.Combobox. If so, you can safely get the picklist values.