0

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

4 Answers 4

0

check this vf page and controller, vf page :

    <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  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;   
        }
    }
}
1
  • there is no need to use wrapper class since we can directly use list of contact
    – Ratan Paul
    Oct 11, 2016 at 3:59
0

This is possible with help of a <apex: repeat> to help <apex:pageBlockTable> for iterarting the contacts.

Check code below,

<Apex:pageBlock>
        <apex:pageblockTable value="{! accounts}" var="account">
            <apex:repeat value="{!account.contacts}" var="contact"> 
                <apex:column value="{!contact.name}"/>
                <apex:column value="{!account.name}"/>
           </apex:repeat>
       </apex:pageBlockTable>  
  </Apex:pageBlock>

Just add the columns as required from both Account and Contact and your table is ready.

That's it.

6
  • Hi appreciated, but it goes in two columns what I need would be is as above results. meaning 1 account = 1 contact so if there is a second contact, still it will display the first contact. thank you!
    – Sony PSP
    Oct 9, 2016 at 10:08
  • I didn't get you, If I am not wrong above code will show the same output as you have given in sample expected output. Oct 9, 2016 at 10:15
  • here is my apex code Public void search(){ if(accname!= '') { String strQuery = 'Select Id, FirstName, LastName, '+ 'Account.Name, Account.BillingCountry '+ 'From Contact where Account.Name like \'%'+accname+'%\''; List<Contact> lstContact = Database.query(strQuery); }
    – Sony PSP
    Oct 9, 2016 at 10:42
  • @Sony PSP again after re-evaluating the things and if I understood this correctly, Even if a Account has multiple related Contacts you need to show single Contact related to that Account, so for a Account there will be only one Contact shown irrespective of number of contacts, is that correct? Oct 9, 2016 at 10:46
  • yes, meaning if the account name is named as Account 1 and that account has multiple contact, it will display the following still Account 1(column 1), billingCountry (column 2) Contact 1(column3) next row Account 1(column 1), billingCountry (column 2) Contact 2(column3) - the only changes you can see is the contact and the country, since the account is associated with multiple contacts.
    – Sony PSP
    Oct 9, 2016 at 10:53
0

A separate point to Ratan's answer...

It is best to avoid dynamic SOQL where possible so that:

  • you benefit from compile-time checking of the query
  • the query is easier to read
  • awkward escaping of string delimiters is avoided
  • you avoid the risk of SOQL Injection; SOQL injection can be defended against in dynamic SOQL too, its just that the simplest solution that you start with is likely to not include that defence

For your example:

if (!String.isEmpty(accName)) {
    String accNameLike = '%' + accName + '%';
    accs = [
            select Id, Name, BillingCountry, (
                    select Id, FirstName, LastName
                    from Contacts
                    order by Name
                    limit 100
                    )
            from Account
            where Name like :accNameLike
            order by Name
            limit 100
            ];
}
3
  • Hi I appreciate your thought, but how do I display the contacts in VF Page wherein 1 account = 1 contact so if there are multiple contacts, it will display still the account associated with it. thank you!
    – Sony PSP
    Oct 9, 2016 at 10:19
  • @SonyPSP Its a bit hard to understand exactly what you want. If you want a table of contacts with a couple of Account fields shown you should change the query to be a query on Contact with a few parent Account fields pulled. But if you want a table of Contacts under each Account the query is OK and you could look at nesting the Contact table inside a cell of the Account table is probably the simplest approach.
    – Keith C
    Oct 9, 2016 at 10:33
  • I need is combination of fields of accounts(accountname, country) and contacts(firstname, lastname), wherein 1 account having multiple contacts. but on the table it will display the following still Account 1(column 1), billingCountry (column 2) Contact 1(column3) next row Account 1(column 1), billingCountry (column 2) Contact 2(column3) - the only changes you can see is the contact and the country, since the account is associated with multiple contacts.
    – Sony PSP
    Oct 9, 2016 at 10:58
0

You can query the contact and get the Account info like this way

List<Contact> lstContact {get;set;}
Public void search()
{ 
   lstContact = new List<Contact>();
   if(accname != '') 
   { 
      String strQuery = 'Select Id, FirstName, LastName, '+
                  'Account.Name, Account.BillingCountry '+
                  'From Contact where Account.Name like \'%'+accname+'%\'';

      lstContact =  Database.query(strQuery);
    }
 }

lstContact will store the contact and its parent Account info Now you show this list in pageblocktable

<apex:pageblocktable value={!lstContact} var="con" >
    <apex:column value="{!con.Account.Name}" />
    <apex:column value="{!con.Account.BillingCountry}" />
    <apex:column value="{!con.FirstName +' '+con.LastName }" />
</apex:pageblocktable>
5
  • actually I have tried this a while back but I encounter error like this Error: Unknown property 'AccountStandardController.lstContact'
    – Sony PSP
    Oct 9, 2016 at 9:56
  • @SonyPSP lstContact should be get;set; property then only you can access this list in vf page
    – Ratan Paul
    Oct 9, 2016 at 10:07
  • it doesn't display anything on the table <apex:pageblocktable value="{!lstContact}" var="con" > <apex:column value="{!con.Account.Name}" /> <apex:column value="{!con.Account.BillingCountry}" /> <apex:column value="{!con.FirstName}' '{con.LastName }" /> </apex:pageblocktable>
    – Sony PSP
    Oct 9, 2016 at 10:39
  • Public void search(){ if(accname != '') { String strQuery = 'Select Id, FirstName, LastName, '+ 'Account.Name, Account.BillingCountry '+ 'From Contact where Account.Name like \'%'+accname+'%\''; List<Contact> lstContact = Database.query(strQuery); }}
    – Sony PSP
    Oct 9, 2016 at 10:41
  • @SonyPSP check my updated answer
    – Ratan Paul
    Oct 9, 2016 at 11:39

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .