Displaying records based on the picklist values

Here is the example of Opportunity object, based on Stage Picklist values.

Apex Class:

public class SearchOpportunity {
    public Opportunity opp  {get;set;}
    public List<Opportunity> OpportunityList  {get;set;}
    
    public SearchOpportunity () {
        opp = new Opportunity();
    }
    
    public PageReference doSearch() {
        OpportunityList =[Select Id, Name, StageName FROM Opportunity WHERE StageName =: Opp.StageName];
        return null;
    }
}

Visualforce Page:

<apex:page controller="SearchOpportunity">
    <apex:form>
        <apex:pageBlock Title="Search Opportunities">
            <b>Stage :</b>
            <apex:inputField value="{!opp.StageName}" />
            <apex:commandButton value="Search" action="{!doSearch}"/>
        </apex:pageBlock>
        
        <apex:pageBlock title="List of Opportunities">
            <apex:pageBlockSection columns="1">
                <apex:pageBlockTable value="{!OpportunityList}" var="item">
                    <apex:column value="{!item.Name}" headerValue="Name"/>
                    <apex:column value="{!item.StageName}" headerValue="Stage"/>
                </apex:pageBlockTable>
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Output:

SearchOpportunities

Leave a Reply