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
NLTNLT 

There is an object named X and in it one picklist field Y with values A,B C and i want to display only 2 picklist values(B, C) when the field is selected in visualforce page?

Santosh Kumar 275Santosh Kumar 275

You can use Dynamic apex for retriving the picklist value. Then while adding picklist value in SelectOption you can use the filter. i.e You can set filter accoring to your crietria what all value you want to add in picklist.

Lets say we have a custom object called Object__c. Object__c contains a number of fields, one of which is a picklist of country values called, creatively enough, CountryPicklist__c.

You can add this code in your controller for fetching all values.

public List<SelectOption> getCountries()
{
  List<SelectOption> options = new List<SelectOption>();
        
   Schema.DescribeFieldResult fieldResult =
 Object__c.CountryPicklist__c.getDescribe();
   List<Schema.PicklistEntry> ple = fieldResult.getPicklistValues();
        
   for( Schema.PicklistEntry f : ple)
   {
/* Here while adding picklist values in option set your filter using some logic like if-else*/
      options.add(new SelectOption(f.getLabel(), f.getValue()));
   }       
   return options;
}
Now we can call the getCountries() method from our Visualforce page,  and populate the <apex:selectList> tag:
<apex:selectList id="countries" value="{!Object__c.CountryPicklist__c}"
         size="1" required="true">
  <apex:selectOptions value="{!countries}"/>
</apex:selectList>

As common practicse if your question is answered marked it as Best answer.