I wrote a generic method that takes an object, field and value as string parameters and returns the picklist label.
I was wondering if there is a more efficient way to write the method instead of returning all fields in a map from the sObject and then looping over to find the specific field?
Here is the method:
public static String getSobjectPicklistLabel(string obj, string fld, string value) {
String retval = '';
Map<String , Schema.SObjectType> globalDescription = Schema.getGlobalDescribe();
Schema.sObjectType sObjType = globalDescription.get(obj);
Schema.DescribeSObjectResult sObjectResult = sObjType.getDescribe();
Map<String , Schema.SObjectField> mapFieldList = sObjectResult.fields.getMap();
for(Schema.SObjectField field : mapFieldList.values()) {
Schema.DescribeFieldResult fieldResult = field.getDescribe();
if(fieldResult.getLabel() == fld) {
List<Schema.PicklistEntry> ple = fieldResult.getPicklistValues();
for(Schema.PicklistEntry f : ple) {
if(f.getValue() == value) {
retval = f.getLabel();
break;
}
}
}
}
return retval;
}