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
Rahul Chauhan 33Rahul Chauhan 33 

i want to make make VF page where i have to display all accounts with their contacts . Query must on contact.

i am trying as below : (CLASS)
public class Displaycontactbyaccname {
   public Map <string,List<Contact>> contactMap {get;set;}
public Displaycontactbyaccname() {
      contactMap = new Map < string, List < Contact >> ();
        List<Contact> conlist = [Select LastName,Account.Name from Contact Where Account.Name != null ];
        System.debug('conn'+conlist[0].Account.Name);
         for (Contact con : conlist)
       {
          if(contactMap.containskey(con.account.Name))
          contactMap.get(con.account.Name).add(con);
      else
          contactMap.put(con.account.Name,new list<contact>{con});
               } }}


and below VF page is 
<apex:page controller="Displaycontactbyaccname">
 <apex:form >
 <apex:pageBlock >
  <apex:pageBlockTable value="{!contactMap}" var="item"  >
    <apex:column value="{!item}"/>  <!-- key --->
    <apex:column value="{!contactMap[item]}"/>  <!-- Value--->
</apex:pageBlockTable>
</apex:pageBlock>
 </apex:form>
</apex:page>


But output is not right : please help me 
thanks in advance :))
Rahul Chauhan 33Rahul Chauhan 33
I want to disply account name and contact name in VF page , please help me soon
thanks in advance :)))))
Raj VakatiRaj Vakati
Change your code as below 
<apex:page controller="Displaycontactbyaccname">
    <apex:form >
        <apex:pageBlock >
            <apex:pageBlockTable value="{!contactMap}" var="item"  >
                <apex:column value="{!item}"/>  <!-- key --->
                <apex:column value="{!contactMap[item]}"/>  <!-- Value--->
            </apex:pageBlockTable>
        </apex:pageBlock>
    </apex:form>
</apex:page>
 
public class Displaycontactbyaccname {
    public Map <string,List<String>> contactMap {get;set;}
    public Displaycontactbyaccname() {
        contactMap = new Map < string, List < String >> ();
        List<Contact> conlist = [Select Name,Account.Name from Contact Where Account.Name != null ];
        System.debug('conn'+conlist[0].Account.Name);
        for (Contact con : conlist)
        {
            if(contactMap.containskey(con.account.Name))
                contactMap.get(con.account.Name).add(con.Name);
            else
                contactMap.put(con.account.Name,new list<String>{con.Name});
        } 
    }
}

 
Umesh RathiUmesh Rathi
Hi, What i understand by you question is you want to display Accounts/Contacts but want to write query on Contact Object.
Here is my code which you can refer, i have tested it in my org and it is working fine.

Visualforce Page:
<apex:page controller="ContactsVisualforceController" standardStylesheets="false">
    <apex:form>
        <apex:pageBlock>
        <apex:pageBlockSection title="Contacts">
            <apex:pageBlockTable columns="4" value="{!displayContacts}" var="contact" >
                <apex:column headerValue="NAME" value="{!contact.Name}"/>
                <apex:column headerValue="PHONE" value="{!contact.Phone}"/>
                <apex:column headerValue="ACCOUNT NAME" value="{!contact.account.name}"/>
                <apex:column headerValue="ACCOUNT PHONE" value="{!contact.account.phone}"/>
            </apex:pageBlockTable>
        </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Controller:
public with sharing class ContactsVisualforceController {
    public list<Contact> displayContacts {get; set;}
    public ContactsVisualforceController(){
        displayContacts = [select id,name,phone,country__c,account.name,account.Phone from Contact Limit 10];
    }
}
If the number of records are more then you will have to implement pagination.
Hope it helps! 
Please mark as complete is it solves the Purpose​​​​​​​.
Thanks! :)
 
Raj VakatiRaj Vakati
This is the correct option if you dnt want to use the maps 
 
<apex:page controller="Displaycontactbyaccname">
    <apex:form >
        <apex:pageBlock >
            <apex:pageBlockTable value="{!accconts1}" var="item"  >
                <apex:column value="{!item.Name}"/>  <!-- key --->
                <apex:pageBlockTable value="{!item.Contacts}" var="con"  >
                    
                    <apex:column value="{!con.Name}"/>  <!-- key --->
                </apex:pageBlockTable>
            </apex:pageBlockTable>
        </apex:pageBlock>
    </apex:form>
</apex:page>
 
public class Displaycontactbyaccname {
    
    public list<account> accconts1 { get; set; }
    
    public Displaycontactbyaccname() {
        accconts1=[select id,name,(select id,Name from contacts) ,(select name from Opportunities ) from account];
        
    }
}

 
Rahul Chauhan 33Rahul Chauhan 33
Hii
My requirement is to display as follow
Account Name
Contact 1
contact 2

Account Name 2
Contact3
contact4

-------

i want output in multiple row single column...??
Abhishek Mahajan 26Abhishek Mahajan 26
use</br tags>  . You Have to study HTML basics . 
Abhishek Mahajan 26Abhishek Mahajan 26
public class DisplayContactByAccName {
    public Map <string,List<Contact>> contactMap {get;set;}
    public Displaycontactbyaccname() {
        contactMap = new Map < string, List < Contact >> ();
        List<Contact> conlist = [Select Name,LastName,Account.Name from Contact Where Account.Name != null ];
        list<Contact> tempConList; // second list for last output map 
        
        for (Contact con : conlist) {
            if(contactMap.containskey(con.account.Name)) {
                tempConList = contactMap.get(con.account.Name);
                tempConList.add(con);
                } else {
                tempConList = new list<contact>();
                tempConList.add(con);
            }
              contactMap.put(con.account.Name,tempConList);
        }
    }
}

 
Umesh RathiUmesh Rathi
Hi,
Finally after so much of research, i have found a way to meet your requirement. Please check if it solves your purpose.

Visualforce:
<apex:page controller="ContactsVisualforceController" standardStylesheets="false">
    <apex:pageBlock>
        <apex:repeat value="{!displayAccounts}" var="acc">
            <dl>
                <dt>Account Name:</dt>
                <dd><apex:outputText  value="{!acc.Name}"/></dd> 
            </dl>
            
            <dl><dt>Contacts:</dt></dl>
            
            <apex:repeat value="{!acc.Contacts}" var="cont">
                <dl>
                    <dd><apex:outputText value="{!cont.Name}"/></dd>
                </dl>
            </apex:repeat>    
        </apex:repeat>
        
    </apex:pageBlock>
</apex:page>

Controller:
public with sharing class ContactsVisualforceController {
    public list<Account> displayAccounts {get; set;}
    public ContactsVisualforceController(){
        displayAccounts = [select id,name,(select id,name from Contacts) from Account];
    }
}

I hope this will help! :)