• aurelioc
  • NEWBIE
  • 0 Points
  • Member since 2011

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 1
    Replies
Below is a class to get all picklists and their values for an object, but I feel like there must be a more efficient way to do it. The main question is, given the name of the object as a string property, do I have to create list<Schema.SObjectType> gd = Schema.getGlobalDescribe().Values() and then loop through all items in the list until I get to the object I want? Or is there a better way to do go directly to the one I want?

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


public class GetPicklistsForObject {

  public string theObjectType {get; set;} //the object we're going to get the picklists for
    
  public List<cObjects> getMyPicklist() {
    
    list<Schema.SObjectType> gd = Schema.getGlobalDescribe().Values(); 
    list<cObjects> myobjects = new list<cObjects>();  

    for(Schema.SObjectType f : gd) { //for all objects in the schema

       if (f.getDescribe().getLabel() == theObjectType) { //we've found the one we're looking for
         
            Map<String, Schema.SObjectField> theFieldmap = f.getDescribe().fields.getMap(); //field map for object
            
            for(String field: theFieldmap.keyset()){ //for each field in the object
              
                   Schema.DisplayType theResult = theFieldmap.get(field).getDescribe().GetType(); //get field type

                   if (theResult == Schema.DisplayType.Picklist){ //if it's a picklist
                     
                          //fill list with picklist values
                           list<Schema.PicklistEntry> pick_list_values = theFieldmap.get(field).getDescribe().getPickListValues();
                            
                   for (Schema.PicklistEntry a : pick_list_values) {  
                        
                        //for each value, add item to myobjects list
                        cObjects tempObject = new cObjects();
                        tempObject.myObjectName = f.getDescribe().getLabel();
                    tempObject.myFieldName = theFieldmap.get(field).GetDescribe().Name;
                    tempObject.myFieldValue = a.getValue();
                    myobjects.add(tempObject);

                   }
                   } //if it's a picklist

            } //for loop
       } //if it matches theObjectType
    }
    return myobjects;
    
  }
  
  public class cObjects {
     public String myObjectName {get; set;}
      public String myFieldName {get; set;}
      public String myFieldValue {get; set;} 
  }

}
Below is a class to get all picklists and their values for an object, but I feel like there must be a more efficient way to do it. The main question is, given the name of the object as a string property, do I have to create list<Schema.SObjectType> gd = Schema.getGlobalDescribe().Values() and then loop through all items in the list until I get to the object I want? Or is there a better way to do go directly to the one I want?

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


public class GetPicklistsForObject {

  public string theObjectType {get; set;} //the object we're going to get the picklists for
    
  public List<cObjects> getMyPicklist() {
    
    list<Schema.SObjectType> gd = Schema.getGlobalDescribe().Values(); 
    list<cObjects> myobjects = new list<cObjects>();  

    for(Schema.SObjectType f : gd) { //for all objects in the schema

       if (f.getDescribe().getLabel() == theObjectType) { //we've found the one we're looking for
         
            Map<String, Schema.SObjectField> theFieldmap = f.getDescribe().fields.getMap(); //field map for object
            
            for(String field: theFieldmap.keyset()){ //for each field in the object
              
                   Schema.DisplayType theResult = theFieldmap.get(field).getDescribe().GetType(); //get field type

                   if (theResult == Schema.DisplayType.Picklist){ //if it's a picklist
                     
                          //fill list with picklist values
                           list<Schema.PicklistEntry> pick_list_values = theFieldmap.get(field).getDescribe().getPickListValues();
                            
                   for (Schema.PicklistEntry a : pick_list_values) {  
                        
                        //for each value, add item to myobjects list
                        cObjects tempObject = new cObjects();
                        tempObject.myObjectName = f.getDescribe().getLabel();
                    tempObject.myFieldName = theFieldmap.get(field).GetDescribe().Name;
                    tempObject.myFieldValue = a.getValue();
                    myobjects.add(tempObject);

                   }
                   } //if it's a picklist

            } //for loop
       } //if it matches theObjectType
    }
    return myobjects;
    
  }
  
  public class cObjects {
     public String myObjectName {get; set;}
      public String myFieldName {get; set;}
      public String myFieldValue {get; set;} 
  }

}