+ Start a Discussion
ChandanBiswasChandanBiswas 

show related contact and opportunity if I select any accout

Here the account shouls be a self lookup with account. Need to display related contact and opportunity if I select any accout(lookup) in a VF page. Can anyone help with the code please.
Best Answer chosen by ChandanBiswas
ChandanBiswasChandanBiswas
@Ajay K Dubedi,
                        I needed a lookup. Your solution doesn't work for me. I got the solution. Thanks for your response. 

All Answers

Ajay K DubediAjay K Dubedi
Hi Chandan,
Please try the following:-
<apex:page controller="ShowAccWithContactController" sidebar="true" showHeader="false">
    <apex:form >
        <apex:pageBlock title="Accounts List">
          <b>  <apex:outputLabel >On Clicking the Account Name you will see the Contact associated..</apex:outputLabel></b><br/><br/>
        <apex:repeat value="{!accounts}" var="acc">
           <b> <apex:commandLink value="{!acc.Name}" action="{!showContacts}">
                 <apex:param name="accountId" value="{!acc.id}" assignTo="{!accountId}"/>
                 <!--apex:outputText value="{!acc.shippingcity}"></apex:outputText-->
            </apex:commandLink></b>
            <br/><br/>
        </apex:repeat>
               
    </apex:pageBlock>
    <apex:pageBlock title="Contacts List" id="ContactBlock" rendered="{!conenable}"> 
        <!--apex:pageBlockTable value="{!contacts}" var="con"-->
        <apex:repeat value="{!contacts}" var="con">
            <apex:outputLabel rendered="{!enable}">
            <apex:commandLink value="{!con.name}">
            <apex:param name="contactId" value="{!con.id}" assignTo="{!contactId}"/>
                </apex:commandLink></apex:outputLabel>
            <apex:outputLabel value="{!con.name}" rendered="{!disable}"></apex:outputLabel>
            <!--apex:column value="{!con.name}" headerValue="Contact Name" ></apex:column-->
            <!--/apex:pageBlockTable--><br/>
            </apex:repeat>
    </apex:pageBlock>
        <apex:pageBlock title="Opp List" id="OppBlock" rendered="{!conenable}"> 
        <!--apex:pageBlockTable value="{!contacts}" var="con"-->
        <apex:repeat value="{!Opps}" var="op">
            <apex:outputLabel rendered="{!enable}">
            <apex:outputField  value="{!op.name}">
                </apex:outputField></apex:outputLabel>
        
            <!--apex:column value="{!con.name}" headerValue="Contact Name" ></apex:column-->
            <!--/apex:pageBlockTable--><br/>
            </apex:repeat>
    </apex:pageBlock>
</apex:form>
</apex:page>
 
public class ShowAccWithContactController {
    public boolean enable{get;set;}
        public boolean conenable{get;set;}
    public boolean disable{get;set;}
    public list<account> accounts{get;set;}
    public String accountId {get; set;}
    public String contactId {get; set;}
    public list<Contact> contacts{get;set;}
        public list<Opportunity> Opps{get;set;}

 public ShowAccWithContactController()
    {
        string acc='select id,name,shippingcity from account limit 20';
        accounts=database.query(acc);
     //   system.assert(false,'Acc::::'+accounts);
    enable=true;
        conenable=false;
        disable=false;
    }
    
    public PageReference showContacts() {
        conenable=true;
        enable=true;
        disable=false;
        contacts = [Select id, name from Contact where AccountId = :accountId];
        Opps= [Select id, name from Opportunity where AccountId = :accountId];
        //system.assert(false,'Acc::::'+accountId); 
        return null;
    }
}

Please mark it as Best if it helps.

Thanks
Ajay
ChandanBiswasChandanBiswas
@Ajay K Dubedi,
                        I needed a lookup. Your solution doesn't work for me. I got the solution. Thanks for your response. 
This was selected as the best answer