0

I got the following variables(Custom Labels referring to picklist values), and a get method to retrieve List of picklist values of a field.

I need to retrieve those List of picklist values in a Set. How can I do this ?

  public String dailyValue {
      get { return System.Label.Daily.toLowerCase().trim(); } 
      private set;
  }
  public String weeklyValue {
      get { return System.Label.Weekly.toLowerCase().trim(); }
      private set;
  }
  public String monthlyValue {
      get { return System.Label.Monthly.toLowerCase().trim(); }
      private set;
  }
  public String quarterlyValue {
      get { return System.Label.Quarterly.toLowerCase().trim(); }
      private set;
  }
  public String biannuallyValue {
      get { return System.Label.Bi_Annually.toLowerCase().trim(); }
      private set;
  }
  public String annuallyValue {
      get { return System.Label.Annually.toLowerCase().trim(); } 
      private set;
  }

  //values from the frequency picklist
  public List<SelectOption> getMonitoringFrequency(){

       List<SelectOption> frequencyTypes = new List<SelectOption>();
       Schema.DescribeFieldResult fieldResult = Metric__c.Monitoring_Frequency__c.getDescribe();
       List<Schema.PicklistEntry> ple = fieldResult.getPicklistValues();
       for( Schema.PicklistEntry f : ple){
            frequencyTypes.add(new SelectOption(
                 f.getValue().toLowerCase(),f.getLabel().toLowerCase()
            ));
       }       
       return frequencyTypes;
   }
1
  • You can always convert from a list to a set with Set<String> mySet = new Set<String> (myList); where myList is a list of String – cropredy Jan 22 '15 at 17:36
4

Depending on whether you want the label or the value in your set, create a set:

set<String> picklistValues = new set<String>();
set<String> picklistLabels = new set<String>():

and then in your for-loop, add the value or label, as appropriate:

for( Schema.PicklistEntry f : ple){
    frequencyTypes.add(new 
        SelectOption(f.getValue().toLowerCase(),f.getLabel().toLowerCase()));
    picklistValues.add(f.getValue());
    picklistLabels.add(f.getLabel());
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.