+ Start a Discussion
YashhYashh 

help on visualforce

i want to create visualforce page to show all accounts name and their related contacts name  through tabular format
Best Answer chosen by Yashh
Suraj Tripathi 47Suraj Tripathi 47

Hi,

try this below code.

<apex:page controller="AccountRelatedContacts">
      <apex:form >
    <apex:pageBlock>
    
     <table border="1px" cellpadding = "0" cellspacing = "0">
                    <tr>
                        
                        <th>Account Name</th>
                        <th>Contacts</th>
                    </tr>
            <apex:repeat value="{!Accounts}" var="a" >
                    <tr>
                        
                        <td>{! a.Name}</td>
                        <td><apex:repeat value="{!a.Contacts}" var="cont">
                <dl>
                    <dd><apex:outputText value="{!cont.Name}"/></dd>
                </dl>
            </apex:repeat>   </td>
                    </tr>
            </apex:repeat>
                    </table>
    
     </apex:pageBlock>
        </apex:form>
</apex:page>
 
public class AccountRelatedContacts {

     public List<Account> getAccounts(){
        list<Account> displayAccounts = [select id,name,(select id,name from Contacts where accountid!=null) from Account];
        return displayAccounts;
    }
}


Please mark it as The Best Answer

Thank You

All Answers

AbhinavAbhinav (Salesforce Developers) 
Hi Yashh,

You can take reference from below link to display related contact for a account.

http://salesforcecodes.com/display-related-contacts-of-an-account-using-visualforce-page/

Thanks!
Suraj Tripathi 47Suraj Tripathi 47

Hi Yash,

Please find the solution. "visualforce"

<apex:page controller="AccountRelatedContacts">
<apex:form>
<apex:pageBlock title="Contacts List" id="contacts_list">
<apex:pageBlockTable value="{! contacts }" var="ct">
 <apex:column value="{! ct.Account.Name }"/>
<apex:column value="{! ct.FirstName }"/>
<apex:column value="{! ct.LastName }"/>
 <apex:column value="{! ct.Email }"/>
</apex:pageBlockTable>
</apex:pageBlock>
</apex:form>
</apex:page>
public class AccountRelatedContacts {
public List<Contact> getContacts() {
List<Contact> conresults = [Select Id,Account.Name, FirstName, LastName,Email from Contact where accountid!=null limit 30];
return conresults;
}
}


Please mark it as the best answer if it helps you.

Thank you

YashhYashh
Hi suraj

i want to show account name as well as account contact name 
and all are  inn the tabular format.
so parents is account
Suraj Tripathi 47Suraj Tripathi 47

Hi,

try this below code.

<apex:page controller="AccountRelatedContacts">
      <apex:form >
    <apex:pageBlock>
    
     <table border="1px" cellpadding = "0" cellspacing = "0">
                    <tr>
                        
                        <th>Account Name</th>
                        <th>Contacts</th>
                    </tr>
            <apex:repeat value="{!Accounts}" var="a" >
                    <tr>
                        
                        <td>{! a.Name}</td>
                        <td><apex:repeat value="{!a.Contacts}" var="cont">
                <dl>
                    <dd><apex:outputText value="{!cont.Name}"/></dd>
                </dl>
            </apex:repeat>   </td>
                    </tr>
            </apex:repeat>
                    </table>
    
     </apex:pageBlock>
        </apex:form>
</apex:page>
 
public class AccountRelatedContacts {

     public List<Account> getAccounts(){
        list<Account> displayAccounts = [select id,name,(select id,name from Contacts where accountid!=null) from Account];
        return displayAccounts;
    }
}


Please mark it as The Best Answer

Thank You

This was selected as the best answer