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
Sony PSPSony PSP 

Joining Account Field and Contact Fields

hope you can help me, how can I display in a table a both the account and contacts fields in VF Page?

Regards

Currently I have this in a method since I call this in button

if(accname !=  ' ')
        {
            string srcqry = 'SELECT Name,BillingCountry,(Select Id, FirstName, LastName From Contacts) FROM Account where name like \'%'+accname+'%\'';
            acc= Database.query(searchquery);
        }
          
how to display the the contacts together with accountname with billingcountry in VF Page without contacts not using repeating instead 1 account = 1contact per display in row, meaninf if there are many contacts in a single account it will display 1 unique account = 1 associated contacts.


Something like this, apologies, still in the stage of learning

AccountName        BillingCountry     ContactName
Unique  1                    US                  John Trevor
Unique  1                    Spain               Paul Blake
Unique  1                   India                  Peter Pan
Amit GhadageAmit Ghadage
Hi Sony PSP,
You can use AccountWrapper to Wrap Account and Contact Details in single record.
Please refer following vf page and controller
 
<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>
Controller :
public class SampleVfDisplayAccountNContactController
{
    public  List<Account> acc{get; set;}
    public  List <AccountWrapper> AccountWrapperList{get;set;}
    public   void loadAccounts()
    {
    String accname = 'Amit';
        AccountWrapperList = new List<AccountWrapper>();
    if(accname !=  ' ')
        {
                   string srcqry = 'SELECT Name,BillingCountry,(Select Id, FirstName, LastName From Contacts) FROM Account where name like \'%'+accname+'%\'';            acc= Database.query(srcqry);
        }
        for(Account a : acc)
        {
            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;   
        }
    }
}

Hope it will help you.
If still you have any doubt feel free to ask.

Best Regards,
Amit Ghadage