+ Start a Discussion
Sagar Bhuwad 9Sagar Bhuwad 9 

how to show single account name and its related contact list on VF page?

Best Answer chosen by Sagar Bhuwad 9
Vinod ChoudharyVinod Choudhary
Hi Sudhir,

Try this:
 
public class accountContactInformation
{
    list<AccountWrapper> wraplist=new list<AccountWrapper>();
    public accountContactInformation()
    {
       Map<Id, AccountWrapper> accMap=new Map<Id, AccountWrapper>();
       for(account a:[SELECT createdby.name,CreatedbyID,Account.Name,(SELECT      
                name,Contact.FirstName, Contact.LastName 
                FROM Account.Contacts) FROM Account])
       {
     AccountWrapper accWrap=accMap.get(a.CreatedByid);  
          // AccountWrapper accWrap=accMap.get(a.CreatedBy.name);
           if (null==accWrap)
           {
              accWrap=new AccountWrapper();
              accMap.put(a.CreatedByid, accWrap);
              accWrap.userid=a.CreatedByid;
           
             // accMap.put(a.CreatedBy.name, accWrap);
              accWrap.username=a.CreatedBy.name;
           }
           accWrap.accounts.add(a);
       }
       wrapList=accMap.values();
   }
   public list<AccountWrapper> getaccounts()
   {
      return wraplist;
   }
   public class AccountWrapper
   {
  public id userid {get; set;} 
      public string username {get; set;}
      public List<Account> accounts {get; set;}
      public AccountWrapper()
      {
         accounts=new List<Account>();
      }
   }
}

And VF page:
 
<table>
  <apex:repeat value="{!accs}" var="acc">
    <tr>
      <td><apex:outputText value="{!acc.Name}"/></td>
      <apex:repeat value="{!acc.Contacts}" var="cont">
         <td><apex:outputText value="{!cont.Name}"/></td>
      </apex:repeat>
    </tr>
  </apex:repeat>
</table>

Thanks
Vinod 

All Answers

RKSalesforceRKSalesforce
Hi Sagar,

Please find below code :
<apex:page standardController="Account">
  
      <h1>Account : </h1>{!Account.Name}
      
      <apex:pageBlock title="Contacts">
        <apex:pageBlockTable value="{!Account.Contacts}" var="con" columns="2" title="Contacts" headerClass="Contacts">
          <apex:column value="{!con.FirstName}"/>
          <apex:column value="{!con.LastName}"/>
      </apex:pageBlockTable>
      </apex:pageBlock>
      
      
</apex:page>

PLease mark as best answer if helped.

Regards,
Ramakant​
Vinod ChoudharyVinod Choudhary
Hi Sudhir,

Try this:
 
public class accountContactInformation
{
    list<AccountWrapper> wraplist=new list<AccountWrapper>();
    public accountContactInformation()
    {
       Map<Id, AccountWrapper> accMap=new Map<Id, AccountWrapper>();
       for(account a:[SELECT createdby.name,CreatedbyID,Account.Name,(SELECT      
                name,Contact.FirstName, Contact.LastName 
                FROM Account.Contacts) FROM Account])
       {
     AccountWrapper accWrap=accMap.get(a.CreatedByid);  
          // AccountWrapper accWrap=accMap.get(a.CreatedBy.name);
           if (null==accWrap)
           {
              accWrap=new AccountWrapper();
              accMap.put(a.CreatedByid, accWrap);
              accWrap.userid=a.CreatedByid;
           
             // accMap.put(a.CreatedBy.name, accWrap);
              accWrap.username=a.CreatedBy.name;
           }
           accWrap.accounts.add(a);
       }
       wrapList=accMap.values();
   }
   public list<AccountWrapper> getaccounts()
   {
      return wraplist;
   }
   public class AccountWrapper
   {
  public id userid {get; set;} 
      public string username {get; set;}
      public List<Account> accounts {get; set;}
      public AccountWrapper()
      {
         accounts=new List<Account>();
      }
   }
}

And VF page:
 
<table>
  <apex:repeat value="{!accs}" var="acc">
    <tr>
      <td><apex:outputText value="{!acc.Name}"/></td>
      <apex:repeat value="{!acc.Contacts}" var="cont">
         <td><apex:outputText value="{!cont.Name}"/></td>
      </apex:repeat>
    </tr>
  </apex:repeat>
</table>

Thanks
Vinod 
This was selected as the best answer