+ Start a Discussion
CBBsfdcCBBsfdc 

Design a Visualforce Page to display Opportunities by Stage. User should be able to select a Stage from a Dropdown on the Page and view all the opportunities of the selected stage

Best Answer chosen by CBBsfdc
Khan AnasKhan Anas (Salesforce Developers) 
Hi,

Greetings to you!

Below is the sample code which I have tested in my org and it is working fine. Kindly modify the code as per your requirement.

Visualforce:
<apex:page controller="OppBasedOnStageC" >
    <apex:form >
        <apex:pageBlock >
            <apex:pageBlockSection >
                <apex:inputField value="{!opp.StageName}" >
                    <apex:actionSupport event="onchange" action="{!showOpp}" reRender="pbt"/>
                </apex:inputField>
            </apex:pageBlockSection>
            <apex:pageBlockTable value="{!opportunities}" var="a" id="pbt">
                <apex:column value="{!a.Name}" />
                <apex:column value="{!a.StageName}" />
            </apex:pageBlockTable>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Controller:
public class OppBasedOnStageC {

    public Opportunity opp {get;set;}
    public List<Opportunity> opportunities {get;set;}
    
    public OppBasedOnStageC() {
        opp = new Opportunity();
		opportunities = new List<Opportunity>();
    }
    
    public void showOpp() {
        if(opp.StageName == 'Prospecting'){
            opportunities = [SELECT Id, Name, StageName FROM Opportunity WHERE StageName='Prospecting'];
        }
        if(opp.StageName == 'Qualification'){
            opportunities = [SELECT Id, Name, StageName FROM Opportunity WHERE StageName='Qualification'];
        }
        if(opp.StageName == 'Need Analysis'){
            opportunities = [SELECT Id, Name, StageName FROM Opportunity WHERE StageName='Need Analysis'];
        }
        if(opp.StageName == 'Need Analysis'){
            opportunities = [SELECT Id, Name, StageName FROM Opportunity WHERE StageName='Need Analysis'];
        }
        if(opp.StageName == 'Closed Won'){
            opportunities = [SELECT Id, Name, StageName FROM Opportunity WHERE StageName='Closed Won'];
        }
        if(opp.StageName == 'Closed Lost'){
            opportunities = [SELECT Id, Name, StageName FROM Opportunity WHERE StageName='Closed Lost'];
        }
        // Add more Stages
    }
}

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas