1

I created an apex method that dynamically gets and displays all the values of a picklist field as options based on a criteria (criteria 1)

Values: Value1, Value2

But there are other two requirements which I am having some difficulty solving with:

  1. when criteria 2 is achieved, the picklist must only display Value 1 as the option

  2. when criteria 3 is achieved, the picklist must only display Value 2 as the option

How can I do this?

Meanwhile, here's the method I created for criteria 1:

public List<SelectOption> getValues()
{
   List<SelectOption> options = new List<SelectOption>();
   Schema.DescribeFieldResult fieldResult =
   Object1__c.Picklist_Field__c.getDescribe();
   List<Schema.PicklistEntry> ple = fieldResult.getPicklistValues();

   for( Schema.PicklistEntry f : ple)
   {
      options.add(new SelectOption(f.getLabel(), f.getValue()));
   }       
   return options;
}

1 Answer 1

1

Assuming you will always have only 2 values in the picklist

public List<SelectOption> getValues()
{
    List<SelectOption> options = new List<SelectOption>();
    Schema.DescribeFieldResult fieldResult =
        Object1__c.Picklist_Field__c.getDescribe();
    List<Schema.PicklistEntry> ple = fieldResult.getPicklistValues();
    // test for criteria 1
    if(**condition 1**){
        options.add(new SelectOption(ple[0].getLabel(),ple[0].getValue()));
        return options;
    }

    // test for criteria 2
    if(**condition 2**)
    {
        options.add(new SelectOption(ple[1].getLabel(),ple[1].getValue()));
        return options;
    }

  // Else return both the values

    for( Schema.PicklistEntry f : ple)
    {
        options.add(new SelectOption(f.getLabel(), f.getValue()));
    }       
    return options;
}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .