I want to display contact and opportunity related to account i.e) when account name is selected from dropdown i need to display related records in VF page using wrapper class. How to achieve this? Could you please anyone post the code?
Below is the code I developed to display Opportunity
, but how to display both Contact
and Opportunity
?
<apex:page controller="WrapperOpp" >
<apex:form >
<apex:pageBlock >
<apex:pageBlockSection >
<apex:selectList value="{!SelectedValue}" size="1">
<apex:selectOptions value="{!Accs}"/>
<apex:actionSupport event="onchange" action="{!refresh}" reRender="OppTable"/>
</apex:selectList>
<apex:pageBlockTable value="{!Opplist}" id="OppTable" var="o">
<apex:column value="{!o.opp.Name}"/>
</apex:pageBlockTable>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>
Wrapper Class
public class WrapperOpp {
public List<OppWrapper> Opplist = new List<OppWrapper>();
public String SelectedValue {get;set;}
public List<SelectOption> Accs {get{
List<SelectOption> AccName = new List<SelectOption>();
for(Account a :[Select Id, name from Account limit 10])
{
AccName.add(new SelectOption(a.name,a.name));
}
return AccName;
}
}
public PageReference refresh()
{
Opplist .clear();
for(Account a :[Select id,name,(Select name from opportunities) from Account where name =:SelectedValue])
{
for (opportunity opp :a.opportunities)
Opplist.add(new OppWrapper(false,opp));
}
return null;
}
public List<OppWrapper> getOppList()
{
System.debug('count'+Opplist.size());
return Opplist;}
public class OppWrapper{
public Boolean selected{get;set;}
public Opportunity opp{get;set;}
public OppWrapper(Boolean selected1, Opportunity opp1)
{
selected = selected1;
opp = opp1;
}
}
}