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
vishal yadav 86vishal yadav 86 

Display accounts and related contacts on pageblock table

I need to display accounts and related contacts using single SOQL query on pagebock table.
How to acheive this?
Best Answer chosen by vishal yadav 86
AnudeepAnudeep (Salesforce Developers) 
Hi Vishal, 

You need to write a relationship SOQL to achieve this. Here is a sample code
 
<apex:page controller="SampleVfDisplayAccountNContactController" action="{!loadAccounts}">
<apex:form >
    <apex:pageBlock id="details">
    <apex:pageBlockTable id="table" value="{!AccountWrapperList}" var="aw">
     <apex:column value="{!aw.AccountName}"/>
    <apex:column value="{!aw.BillingCountry}"/>AccountName
    <apex:column value="{!aw.ContactName}"/>
    </apex:pageBlockTable>

    </apex:pageBlock>
</apex:form>
</apex:page>
 
public class SampleVfDisplayAccountNContactController
{
    public  String accname{get; set;}
    public  List <AccountWrapper> AccountWrapperList{get;set;}
    public   void loadAccounts()
    {

        AccountWrapperList = new List<AccountWrapper>();
    if(accname !=  ' ')
        {
                   string srcqry = 'SELECT Name,BillingCountry,(Select Id, FirstName, LastName From Contacts) FROM Account where name like \'%'+accname+'%\'';            
        }
        for(Account a : Database.query(srcqry))
        {
            for(Contact c : a.Contacts)
            {
                AccountWrapper aw = new AccountWrapper(a.name, a.BillingCountry,c.FirstName + c.LastName);
                AccountWrapperList.add(aw);

            }
        }
        System.debug('AccountWrapperList'+AccountWrapperList);
    }

    public class AccountWrapper{
    public  String AccountName{get; set;}
    public  String BillingCountry{get; set;}
     public  String ContactName{get; set;}
        AccountWrapper(String AccountName,String BillingCountry, String ContactName)
        {
            this.AccountName = AccountName;
            this.BillingCountry = BillingCountry;
            this.ContactName = ContactName;   
        }
    }
}

This is taken from this post

If you find this information helpful. Please mark this answer as best. It may help others in the community. Thank You!

Anudeep

All Answers

AnudeepAnudeep (Salesforce Developers) 
Hi Vishal, 

You need to write a relationship SOQL to achieve this. Here is a sample code
 
<apex:page controller="SampleVfDisplayAccountNContactController" action="{!loadAccounts}">
<apex:form >
    <apex:pageBlock id="details">
    <apex:pageBlockTable id="table" value="{!AccountWrapperList}" var="aw">
     <apex:column value="{!aw.AccountName}"/>
    <apex:column value="{!aw.BillingCountry}"/>AccountName
    <apex:column value="{!aw.ContactName}"/>
    </apex:pageBlockTable>

    </apex:pageBlock>
</apex:form>
</apex:page>
 
public class SampleVfDisplayAccountNContactController
{
    public  String accname{get; set;}
    public  List <AccountWrapper> AccountWrapperList{get;set;}
    public   void loadAccounts()
    {

        AccountWrapperList = new List<AccountWrapper>();
    if(accname !=  ' ')
        {
                   string srcqry = 'SELECT Name,BillingCountry,(Select Id, FirstName, LastName From Contacts) FROM Account where name like \'%'+accname+'%\'';            
        }
        for(Account a : Database.query(srcqry))
        {
            for(Contact c : a.Contacts)
            {
                AccountWrapper aw = new AccountWrapper(a.name, a.BillingCountry,c.FirstName + c.LastName);
                AccountWrapperList.add(aw);

            }
        }
        System.debug('AccountWrapperList'+AccountWrapperList);
    }

    public class AccountWrapper{
    public  String AccountName{get; set;}
    public  String BillingCountry{get; set;}
     public  String ContactName{get; set;}
        AccountWrapper(String AccountName,String BillingCountry, String ContactName)
        {
            this.AccountName = AccountName;
            this.BillingCountry = BillingCountry;
            this.ContactName = ContactName;   
        }
    }
}

This is taken from this post

If you find this information helpful. Please mark this answer as best. It may help others in the community. Thank You!

Anudeep
This was selected as the best answer
vishal yadav 86vishal yadav 86
Can you ping me here 9030426884
need bit clariication?