+ Start a Discussion
Rabbani sayyed 8Rabbani sayyed 8 

I want to create a VF page that display list of accouts and when i select a particular account i want related contact to be displayed there itself. i dont want page to redirect to another page? Can anyone share me the VF code for this?

I want to create a VF page that display list of accouts and when i select a particular account i want related contact to be displayed there itself. i dont want page to redirect to another page? Can anyone share me the VF code for this?
ManojjenaManojjena
Hi Rabbani,

Have you achieved the first part .Display list of account ? 
Nitin PaliwalNitin Paliwal
Hi Rabbani,
Below is the complete vf page and controller attached. Click on the "Click" link in the Account records to display contact below.
<!--------- VF PAGE -------------->

<apex:page id="pgId" controller="AccountDetails">
    <Apex:form>
        <apex:pageBlock>
            <Apex:pageblockSection title="Accounts">
                <apex:pageBlockTable value="{!accounts}" var="account">
                    <Apex:column headervalue="Account id">
                        <Apex:outputPanel >
                            <Apex:outputText value="Click" />
                            <apex:actionSupport event="onclick" action="{!getContactDetails}" rerender="contactBlock" >
                                <Apex:param name="accountId" assignTo="{!accountId}" value="{!account.Id}"/>
                            </apex:actionSupport>
                                   
                        </apex:outputPanel >
                    </apex:column>
                    <Apex:column headervalue="Account name">
                        <Apex:outputField value="{!account.Name}" />
                    </apex:column>
                </apex:pageBlockTable>
            </Apex:pageblockSection>
        </apex:pageBlock>
       
        <apex:pageBlock id="contactBlock">
            <Apex:pageblockSection title="Contacts">

                <apex:pageBlockTable value="{!contacts}" var="contact">
                    <Apex:column headervalue="Contact id">
                        <Apex:outputField value="{!contact.Id}"/>
                    </apex:column>
                    <Apex:column headervalue="Contact name">
                        <Apex:outputField value="{!contact.Name}" />
                    </apex:column>
                </apex:pageBlockTable>
            </Apex:pageblockSection>

        </apex:pageBlock>

    </apex:form>
</apex:page>

<!---Controller --->
public with sharing class AccountDetails{
        public list<Account> accounts{get;set;}
        public String accountId{get;set;}
        public list<contact> contacts{get;set;}
        public AccountDetails(){
            accounts = [select Id,Name from Account limit 10];
        }
       
        public void getContactDetails(){
            contacts = [select Id,Name from Contact where AccountId =:accountId];
        }
}

I hope it solves your problem, feel free to ask if there is any issue.

Thanks
Nitin
Rabbani sayyed 8Rabbani sayyed 8
Hi Nitin,

I am trying with the code that you have shared but it's giving me the error. please find the below error.

Error: AccountDetails Compile Error: Illegal assignment from List<Account> to List<Account> at line 6 column 1

Regards
Rabbani
Rabbani sayyed 8Rabbani sayyed 8
Hi Manoj,

Yes i tried

Regards
Rabbani
Nitin PaliwalNitin Paliwal
Hi,
Please check do you have an apex class named as"Account" in your org?

Thanks
Nitin
Rabbani sayyed 8Rabbani sayyed 8
No nitin, i dont have any apex class with Account
Nitin PaliwalNitin Paliwal

Hi,
Its working fine in my environment, and the only issue which i can think of if that there must be an apex class named as "Account".
Check this link
https://developer.salesforce.com/forums?id=906F00000008vO5IAI

Thanks
Nitin
 

ManojjenaManojjena
Hi Rabbani,

Try below codean dlet us know any issue .
<apex:page id="pgId" controller="ContactDetailOnClickofAccount">
    <apex:form  id="frm">
        <apex:pageBlock  id="pgblk">
            <Apex:pageblockSection title="Accounts" id="pbsec">
                <apex:pageBlockTable value="{!accounts}" var="account" id="pgtable">
                    <apex:column headervalue="Account name">
                        <apex:commandLink value="{!account.Name}"  ReRender="contactBlock">
                          <apex:actionSupport event="onclick" action="{!getRelatedContact}" rerender="conpgblk" >
                                <apex:param name="accountId" assignTo="{!accountId}" value="{!account.Id}"/>
                            </apex:actionSupport>
                        </apex:commandLink>
                    </apex:column>
                </apex:pageBlockTable>
            </apex:pageblockSection>
        </apex:pageBlock>
        <apex:pageBlock id="conpgblk">
            <apex:pageblockSection title="Contacts">
               <apex:pageBlockTable value="{!contacts}" var="contact">
                     <apex:column headervalue="First Name" value="{!contact.FirstName}" />
                     <apex:column headervalue="Last Name" value="{!contact.lastName}" />
                </apex:pageBlockTable>
            </apex:pageblockSection>
       </apex:pageBlock>
     </apex:form>
</apex:page>
    

public class ContactDetailOnClickofAccount{
        public List<Account> accountList{get;set;}
        public Id accountId{get;set;}
        public list<contact> contactList{get;set;}
        public ContactDetailOnClickofAccount(){
		    accountList=new List<Account>();
			contactList=new List<Conatct>();
            accountList = [SELECT  Id,Name FROM Account LIMIT 500];
        }
        public void getRelatedContact(){
		    contactList = [SELECT Id,firstName,LastName FROM Contact WHERE AccountId =:accountId LIMIT 1000];
       }
}


You can display one msg if account what you have clicked may be related contacts are not there.

Thnaks 
Manoj