Tuesday, 14 February 2017

Retrieve All Picklist Values for specifid fields through APEX


Below code will retrieve all picklist values for specified fields based on specific object



    public static Map<String,List<String>> getPickListValues(){
       
        Map<String,List<String>> mapOptions = new Map<String,List<String>>();      
       
        Schema.sObjectType sobject_type = Account.getSObjectType(); 
       
        Schema.DescribeSObjectResult sobject_describe = sobject_type.getDescribe();
       
        Map<String, Schema.SObjectField> field_map = sobject_describe.fields.getMap(); 
       
        list<String> fieldList = new list<String> 
 {'Field1__c','Field2__c','Field3__c','Field4__c','Field5__c'};
       
        for(String s:fieldList){
           
            List<Schema.PicklistEntry> pick_list_values = field_map.get(s).getDescribe().getPickListValues(); //grab the list of picklist values for the passed field on the sobject
            List<String> sOptions = new List<String>();
            for (Schema.PicklistEntry a : pick_list_values) { //for all values in the picklist list
               
                sOptions.add(a.getValue()); //add the value and label to our final list
               
            }
           
            mapOptions.put(s,sOptions);
       
        }    
       
        return mapOptions;

}

No comments:

Post a Comment