function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
adi salesforceadi salesforce 

How to display list of account records in a dropdown and if we select particular record corresponding contacts, opportunities should come

FARSANA PSFARSANA PS
Hi,

try this..

VF page
<apex:page controller="account_opportunity_controller">
 <apex:form >       
   <apex:selectList value="{!val}"  multiselect="false" size="1">
         <apex:actionSupport event="onchange" action="{!changeList}" reRender="myblck"/>
      <apex:selectOptions value="{!items}">
    </apex:selectOptions>
</apex:selectList>
       <apex:pageBlock id="myblck" >
       <apex:pageBlockSection title="Related Records">
           <apex:pageBlockTable   value="{!acc_final_list}" var="con" >     
               <apex:column >
                 <apex:pageBlockTable value="{!con.contacts}" var="c" > 
                 <apex:column headerValue="contact name"  value="{!c.name}"/>
                   </apex:pageBlockTable> 
               </apex:column >
                 <apex:column >
                     <apex:pageBlockTable value="{!con.opportunities}" var="o" > 
                 <apex:column   headerValue="opportunity name" value="{!o.name}"/>
               </apex:pageBlockTable>
                   </apex:column>
            </apex:pageBlockTable>   
</apex:form>
</apex:page>

Controller
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];           
       }
  
}

adi salesforceadi salesforce
Hi,
Thank you for giving me the solution but I want to display contacts and opportunites in separate blocks in the same page.
FARSANA PSFARSANA PS
Hi,
Try this to display contacts and opportunites in separate blocks in the same page...
  
VF page 
<apex:page controller="account_opportunity_controller">
 <apex:form >       
   <apex:selectList value="{!val}"  multiselect="false" size="1">
         <apex:actionSupport event="onchange" action="{!changeList}" reRender="myblck"/>
      <apex:selectOptions value="{!items}">
    </apex:selectOptions>
</apex:selectList>
   <apex:pageBlock id="myblck">
        <apex:pageBlock>
  <apex:pageBlockSection title="Related contacts">
           <apex:pageBlockTable value="{!con_list}" var="con" >  
               <apex:column headerValue="Contact Name" value="{!con.name}"/>
            </apex:pageBlockTable>  
          </apex:pageBlockSection>
       </apex:pageBlock>
        <apex:pageBlock>
      <apex:pageBlockSection title="Related opportunities">
            <apex:pageBlockTable value="{!opp_list}" var="opp" >  
                <apex:column headerValue="Opportunity Name" value="{!opp.name}"/>
            </apex:pageBlockTable>  
      </apex:pageBlockSection>
       </apex:pageBlock>
   </apex:pageBlock> 
</apex:form>
</apex:page>
Controller
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];
                  
       }
}

Hope this helps...
 
Ajay K DubediAjay K Dubedi
Hi Adi,

You can try the following code. Hope this will help you.

<apex:page controller="AccContactController">
    <apex:form >
        <apex:pageBlock title="Account Name">
            Account Names&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            <apex:selectList value="{!selectedAccId}" size="1">                                 
                <apex:selectOptions value="{!AccountNames}"/>
                <apex:actionSupport event="onchange" reRender="Cont,Oppor"/>
            </apex:selectList>
            
            <br/><br/>
                Contact Names&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                <apex:selectList value="{!selectedConId}" size="1"  id="Cont">
                    <apex:selectOptions value="{!ContactNames}" />
                    <apex:actionSupport event="onchange"/>
                </apex:selectList>       
                <br/><br/>
                
                Opportunity Names&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                <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