how to get a picklist all values in apex controller

  • I have a Student object and have a pick list field "Student Status" in this object. Status can be Full-Time OR Part Time. Now my requirement is i want this student status list, Part-Time or Full-Time in apex controller. how can i do this?

    If i get a student record it will only give me that particular student status Full-Time or Part-Time. But i need all options that are available in student status field.

    Please help me

  • This is very common requirement and we have few good blogs from where you can directly get the code

    Example code:

    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;
    

    Here is the Utility Method i use to get all the picklist values for an object and Field.

    In your case you need select option for the allow user to select if you use in visualforce page

    global static list<SelectOption> getPicklistValues(SObject obj, String fld){
      list<SelectOption> options = new list<SelectOption>();
      // Get the object type of the SObject.
      Schema.sObjectType objType = obj.getSObjectType(); 
      // Describe the SObject using its object type.
      Schema.DescribeSObjectResult objDescribe = objType.getDescribe();       
      // Get a map of fields for the SObject
      map<String, Schema.SObjectField> fieldMap = objDescribe.fields.getMap(); 
      // Get the list of picklist values for this field.
      list<Schema.PicklistEntry> values =
         fieldMap.get(fld).getDescribe().getPickListValues();
      // Add these values to the selectoption list.
      for (Schema.PicklistEntry a : values)
      { 
         options.add(new SelectOption(a.getLabel(), a.getValue())); 
      }
      return options;
     }
    } 
    

    Any idea how to get picklist labels in a different language from the current session?

    In the first snippet, the newSobject part is superfluous, as the only reason it's happening is to get the sObjectType out of it (which is where we started from). I.e., targetType and sobject_type are the same. Just saying :) Otherwise, exactly what I needed.

License under CC-BY-SA with attribution


Content dated before 7/24/2021 11:53 AM