+ Start a Discussion
alok29novalok29nov 

Displaying data on a visualforce page based on the selection of a picklist values

Hi,

 

I have a requirement where I need to display opportunity on a account page. The opportunites that has to be displayed would be based on a filter. Actually the req. is that opportunites should be displayed based on region which will be selected from the dropdown (a vf section on account page) on the account page. Can anybody help me with the code.

 

Thanks in advance!!!

 

 

Best Answer chosen by Admin (Salesforce Developers) 
TheSwamiTheSwami

If you want to use the <apex:detail/> tag to display the Account, that would be the easiest - however, there is not a standard way to attach a behavior to a dropdown with in that page.

 

Here is an example of a solution using a custom drop down list:

 

VF PAGE

<apex:page standardController="Account" extensions="OpportunityFilter">
    <apex:form >
        <apex:selectList value="{!regionValue}" size="1">
           <apex:selectOptions value="{!regionOptions}"/> 
        </apex:selectList>
        <apex:commandButton value="Update Opportunities" action="{!updateFilteredOpportunities}"/>
    </apex:form>
    
    <apex:pageBlock >
        <apex:pageBlockTable value="{!filteredOpportunities}" var="opportunity">
            <apex:column value="{!opportunity.name}"/>
        </apex:pageBlockTable>
    </apex:pageBlock>
</apex:page>

 

CONTROLLER

public with sharing class OpportunityFilter {
    public String regionValue {get;set;}    
    public List<SelectOption> regionOptions {get;set;}
    public List<Opportunity> filteredOpportunities {get;set;}
    
    public OpportunityFilter(ApexPages.StandardController controller) {
        regionOptions = new List<SelectOption>();
        regionOptions.add(new SelectOption('Manufacturing','Manufacturing'));
        regionOptions.add(new SelectOption('Medical','Medical'));
    }
    
    public void updateFilteredOpportunities() {
        filteredOpportunities = [SELECT id,name FROM Opportunity WHERE Account.Industry = :regionValue];
    }

}

 

This uses a query on the Account Industry field, but you could adjust to filter on a custom "region" field quite easily.

Hope this helps!

All Answers

TheSwamiTheSwami

If you want to use the <apex:detail/> tag to display the Account, that would be the easiest - however, there is not a standard way to attach a behavior to a dropdown with in that page.

 

Here is an example of a solution using a custom drop down list:

 

VF PAGE

<apex:page standardController="Account" extensions="OpportunityFilter">
    <apex:form >
        <apex:selectList value="{!regionValue}" size="1">
           <apex:selectOptions value="{!regionOptions}"/> 
        </apex:selectList>
        <apex:commandButton value="Update Opportunities" action="{!updateFilteredOpportunities}"/>
    </apex:form>
    
    <apex:pageBlock >
        <apex:pageBlockTable value="{!filteredOpportunities}" var="opportunity">
            <apex:column value="{!opportunity.name}"/>
        </apex:pageBlockTable>
    </apex:pageBlock>
</apex:page>

 

CONTROLLER

public with sharing class OpportunityFilter {
    public String regionValue {get;set;}    
    public List<SelectOption> regionOptions {get;set;}
    public List<Opportunity> filteredOpportunities {get;set;}
    
    public OpportunityFilter(ApexPages.StandardController controller) {
        regionOptions = new List<SelectOption>();
        regionOptions.add(new SelectOption('Manufacturing','Manufacturing'));
        regionOptions.add(new SelectOption('Medical','Medical'));
    }
    
    public void updateFilteredOpportunities() {
        filteredOpportunities = [SELECT id,name FROM Opportunity WHERE Account.Industry = :regionValue];
    }

}

 

This uses a query on the Account Industry field, but you could adjust to filter on a custom "region" field quite easily.

Hope this helps!

This was selected as the best answer
alok29novalok29nov

Thanks you so much for your detailed reply...I could not check the code but looks like this should work fine for me. Would ask you more questions if I get any issues :) :)

 

Thanks again, TheSwami!!!

alok29novalok29nov

Hi Swami,

 

It worked fine. Thanks!!!

TheSwamiTheSwami

Great - glad to know.  Can you click the "Accept as solution" so others know this worked?  Thanks!

alok29novalok29nov

Sure..I just forgot to click..