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

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;
}
| improve this answer | |

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.