function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Shuhbam SinhaShuhbam Sinha 

How to show picklist filed's value conditionally.

Hello Everyone,
I have one scenario where I need to show picklist values conditionally. I am using lightning record edit form with <lightning-input-field> to show picklist values but i want to hide some of the values . I have a datatable which is showing some records with row selection. Let say A record is selected then have to show only three values out of 6 values of that picklist field or if B record is selected , have to show only 4 values.
Can anyone please help me on the same. Thanks in advance.
VinayVinay (Salesforce Developers) 
Hi Shuhbam,

Check below example to show picklist filed's value conditionally
 
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;
}

Please mark as Best Answer if above information was helpful.

Thanks,