public class account_opportunity_controller {
public String Val{get;set;}
public List<SelectOption> opt {get;set;}
public list<Account> accList {get;set;}
public list<Account> acc_final_list{get;set;}
public account_opportunity_controller()
{
accList=[select id,name from account limit 100];
}
public List<SelectOption> getItems() {
opt = new List<SelectOption>();
accList=[select id,name from account limit 100];
opt.add(new SelectOption('none','--select--'));
for(Account a:accList)
opt.add(new SelectOption(string.valueof(a.id),a.name));
return opt;
}
public void changeList()
{
id accid=id.valueof(Val);
acc_final_list =[select id,name,(select name from contacts),(select name from opportunities) from account where id=: accid];
}
}
public class account_opportunity_controller {
public String Val{get;set;}
public List<SelectOption> opt {get;set;}
public list<Account> accList {get;set;}
public list<Account> acc_final_list{get;set;}
public list<contact> con_list{get;set;}
public list<opportunity> opp_list{get;set;}
public account_opportunity_controller()
{
accList=[select id,name from account limit 100];
}
public List<SelectOption> getItems() {
opt = new List<SelectOption>();
accList=[select id,name from account limit 100];
opt.add(new SelectOption('none','--select--'));
for(Account a:accList)
opt.add(new SelectOption(string.valueof(a.id),a.name));
return opt;
}
public void changeList()
{
id accid=id.valueof(Val);
System.debug(accid);
con_list=new list<contact>();
opp_list=new list<opportunity>();
con_list=[select id, name from contact where accountId=: accid];
opp_list=[select id, name from opportunity where accountId=: accid];
}
}
try this..
VF page
Controller
Thank you for giving me the solution but I want to display contacts and opportunites in separate blocks in the same page.
Try this to display contacts and opportunites in separate blocks in the same page...
VF page Controller
Hope this helps...
You can try the following code. Hope this will help you.
<apex:page controller="AccContactController">
<apex:form >
<apex:pageBlock title="Account Name">
Account Names
<apex:selectList value="{!selectedAccId}" size="1">
<apex:selectOptions value="{!AccountNames}"/>
<apex:actionSupport event="onchange" reRender="Cont,Oppor"/>
</apex:selectList>
<br/><br/>
Contact Names
<apex:selectList value="{!selectedConId}" size="1" id="Cont">
<apex:selectOptions value="{!ContactNames}" />
<apex:actionSupport event="onchange"/>
</apex:selectList>
<br/><br/>
Opportunity Names
<apex:selectList value="{!selectedOppId}" size="1" id="Oppor">
<apex:selectOptions value="{!OpportunityNames}" />
</apex:selectList>
</apex:pageBlock>
</apex:form>
</apex:page>
Controller:
public class AccContactController {
public Id selectedAccId{get;set;}
public Id selectedConId{get;set;}
public Id selectedOppId{get;set;}
public List<Contact> conlist {get;set;}
public List<Opportunity> opplist {get;set;}
public List<SelectOption> getAccountNames() {
List<SelectOption> accOptions= new List<SelectOption>();
system.debug(selectedAccId);
accOptions.add( new SelectOption('','--Select--'));
for(Account acc : [select Id,name from Account ] ) {
accOptions.add(new SelectOption(acc.Id,acc.name));
}
return accOptions;
}
public List<SelectOption> getContactNames() {
System.debug('Entered ContactNames account id...........'+selectedAccId );
List<SelectOption> conOptions= new List<SelectOption>();
List<SelectOption> options = new List<SelectOption>();
if(selectedAccId != null)
{
for( contact con : [select Id,name,accountid from contact where accountid=:selectedAccId ] ) {
conOptions.add( new SelectOption(con.Id,con.name));
}
}
else
{
conOptions.add( new SelectOption('--None--','--None--'));
}
return conOptions;
}
public List<SelectOption> getOpportunityNames()
{
List<SelectOption> oppOptions= new List<SelectOption>();
List<SelectOption> options = new List<SelectOption>();
if(selectedAccId != null)
{
for(Opportunity opp :[Select Id, Name,AccountId FROM Opportunity WHERE AccountId =:selectedAccId])
{
system.debug('opportunity Obj:' + opp);
oppOptions.add( new SelectOption(opp.Id,opp.name));
}
}
else
{
system.debug('opportunity else');
oppOptions.add( new SelectOption('--None--','--None--'));
}
return oppOptions;
}
}
Thank You
Ajay Dubedi