Dynamically Querying PickList for Standdard and Custom Field in Apex

You can refer below sample example in order to get an idea.
VF
--------------------------
<apex:page Controller="DynamicPickListCtrl">
    <apex:form >
        <apex:pageMessages />
        <apex:pageBlock >
            <apex:pageBlockSection columns="2">
                <apex:pageblockSectionItem >
                    <apex:outputLabel value="SLA"/>
                </apex:pageblockSectionItem>
                <apex:pageblockSectionItem >
                    <apex:selectList value="{!SLA}" size="1" >
                        <apex:selectOptions value="{!slaOptiions}"/>
                    </apex:selectList>
                </apex:pageblockSectionItem>
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>
----------------------

Controller
--------------------------------
public class DynamicPickListCtrl {
 public String SLA {
  get;
  set;
 }

 public DynamicPickListCtrl() {

 }

 public List < SelectOption > getslaOptiions() {
  List < SelectOption > options = new List < SelectOption > ();
  options.add(new selectOption('None', '--- None ---'));
  Schema.DescribeFieldResult fieldResult = Account.SLA__c.getDescribe();
  List < Schema.picklistEntry > ple = fieldResult.getPicklistValues();
  for (Schema.picklistEntry f: ple) {
   options.add(new selectOption(f.getLabel(), f.getValue()));
  }
  return Options;
 }
}
-----------------------------------

I hope this will help you.


Generic Method for getting pick as List<String>
---------------------------------------------------------------
 public static List < String > getPicklistValues(String ObjectApi_name, String Field_name) {

  List < String > lstPickvals = new List < String > ();
  Schema.SObjectType targetType = Schema.getGlobalDescribe().get(ObjectApi_name); //From the Object Api name retrieving the SObject
  Sobject Object_name = targetType.newSObject();
  Schema.sObjectType sobject_type = Object_name.getSObjectType(); //grab the sobject that was passed
  Schema.DescribeSObjectResult sobject_describe = sobject_type.getDescribe(); //describe the sobject
  Map < String, Schema.SObjectField > field_map = sobject_describe.fields.getMap(); //get a map of fields for the passed sobject
  List < Schema.PicklistEntry > pick_list_values = field_map.get(Field_name).getDescribe().getPickListValues(); //grab the list of picklist values for the passed field on the sobject
  for (Schema.PicklistEntry a: pick_list_values) { //for all values in the picklist list
   lstPickvals.add(a.getValue()); //add the value  to our final list
  }

  return lstPickvals;
 }

---------------------------------------------------------------


Generic Method for getting pick as List < SelectOption >
---------------------------------------------------------------
 public static List < SelectOption > getPicklistValues(String ObjectApi_name, String Field_name) {

  List < SelectOption > options = new List < SelectOption > ();
  Schema.SObjectType targetType = Schema.getGlobalDescribe().get(ObjectApi_name); //From the Object Api name retrieving the SObject
  Sobject Object_name = targetType.newSObject();
  Schema.sObjectType sobject_type = Object_name.getSObjectType(); //grab the sobject that was passed
  Schema.DescribeSObjectResult sobject_describe = sobject_type.getDescribe(); //describe the sobject
  Map < String, Schema.SObjectField > field_map = sobject_describe.fields.getMap(); //get a map of fields for the passed sobject
  List < Schema.PicklistEntry > pick_list_values = field_map.get(Field_name).getDescribe().getPickListValues(); //grab the list of picklist values for the passed field on the sobject
  for (Schema.PicklistEntry a: pick_list_values) { //for all values in the picklist list
   options.add(new selectOption(a.getLabel(), a.getValue()));
  }

  return options;

 }

---------------------------------------------------------------

I hope this will help you.

Comments

Popular posts from this blog

Custom Setting Creation in Apex Test Class

System.AsyncException: The Apex job named is already scheduled for execution.

Salesforce Trace Log Query Using Apex

AJAX Toolkit Debug Shell

Error : "Trigger must be associated with a job detail"

How to Query All the batch classes in your Salesforce Instance using APEX

Prefix List Of Salesforce Object

how to get object name from id / prefix value in salesforce

Creating Salesforce Developer Account