Salesforce Picklist 通用类

Salesforce Picklist 通用类

Salesforce -> Setup -> Developer Console -> APEX Class

Class Name: PickListValuesUtil

public class PickListValuesUtil {

/*

Get sObjectName and the sObjectName’s subfield(Picklist Type) to PickList’s values

*/

public static Map<String, object> getPicklistValues(String sObjectName, String sFieldName){

Map<String, object> picValues= new Map<String, object>();

Map<String, Schema.SObjectType> sObjectDescribeMap = Schema.getGlobalDescribe();

if (sObjectDescribeMap.containsKey(sObjectName)) {

Map<String, Schema.SobjectField> sObjectFieldsMap =

sObjectDescribeMap.get(sObjectName).getDescribe().fields.getMap();

if (sObjectFieldsMap.containsKey(sFieldName)) {

Schema.DescribeFieldResult sObjectFieldDescribe = sObjectFieldsMap.get(sFieldName).getDescribe();

List<Schema.PicklistEntry> lPickEntryList = sObjectFieldDescribe.getPicklistValues();

for (Schema.PicklistEntry tPicklistEntry : lPickEntryList) {

picValues.put(tPicklistEntry.getValue(), new Map<String,object>{

‘value’ => tPicklistEntry.getValue(),

‘isActive’ => tPicklistEntry.isActive(),

‘isDefaultValue’ => tPicklistEntry.isDefaultValue(),

‘label’ => tPicklistEntry.getLabel()

});

}

} else {

//TODO -> not found the Picklist Field

}

} else {

// TODO not found the sObject

}

return picValues;

}

}

通过debug 直接测试

Map<String,Object> invoice_c = PickListValuesUtil.getPicklistValues(‘Battle_Station__c’,’Project_Status__c’);

System.debug(invoice_c);

 

Leave a Comment