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
Nagarjuna Reddy NandireddyNagarjuna Reddy Nandireddy 

how to display two objects fields in visualforce page by using wrapper class?

Hi all,
  I want to two objects(Account,Contact) fields in vf page by using wrapper class 
Please provide me a simple code for it.

Tanking you,
Nagarjuna Reddy Nandireddy
Best Answer chosen by Nagarjuna Reddy Nandireddy
Martijn SchwarzerMartijn Schwarzer
Hi Nagarjuna Reddy Nandireddy,

Please find below an example of how to accomplish this.

I've included 2 ways to do this:
  1. Using a single query
  2. Using a wrapper class
My controller (AccountContactWrapperController.class):
public class AccountContactWrapperController { 

	public List<Account> accounts{get;set;}
	public List<ACWrapper> ACWrappers{get;set;}

	public AccountContactWrapperController(){
		this.ACWrappers = new List<ACWrapper>();
		this.accounts = new List<Account>();

		//Get Accounts & Related Contacts in 1 query
		List<Account> accs = [Select Id, Name, (Select Id, FirstName, LastName From Contacts) From Account Limit 1];
		this.accounts = accs;

		//Get Accounts & Contacts in separate queries for Wrapper
		List<Account> accsForWrapper = [Select Id, Name From Account Limit 1];
		List<Contact> consForWrapper;
		Account acc;
		if(accsForWrapper != null && !accsForWrapper.isEmpty()){
			acc = accsForWrapper.get(0);
			consForWrapper = [Select Id, FirstName, LastName From Contact Where AccountId =:acc.Id];
		}
		if(consForWrapper != null){
			ACWrapper wrapper = new ACWrapper(acc, consForWrapper);
			this.ACWrappers.add(wrapper);
		}
	}

	public class ACWrapper{
		public Account account{get;set;}
		public List<Contact> contacts{get;set;}

		public ACWrapper(Account acc, List<Contact> cons){
			account = acc;
			contacts = cons;
		}
	}

}

My VF page (AccountContactWrapperTest.page):
<apex:page controller="AccountContactWrapperController" >
    <apex:form >

        <!--Display Account & Contact info from 1 Query-->
        <apex:pageBlock title="Info From Query">
            <apex:pageBlockTable value="{!accounts}" var="a">
                <apex:Column value="{!a.Name}" />
                <apex:Column headerValue="Contact Names">
                    <apex:repeat value="{!a.Contacts}" var="c">
                        {!c.FirstName} {!c.LastName} <br />
                    </apex:repeat>
                </apex:Column>
            </apex:pageBlockTable>
        </apex:pageBlock>

        <!--Display Account & Contact info from 1 Wrapper class-->
        <apex:pageBlock title="Info From Wrapper class">
            <apex:pageblocktable value="{!ACWrappers}" var="w">
                <apex:Column value="{!w.account.Name}" />
                <apex:column headervalue="Contact Names">
                    <apex:repeat value="{!w.contacts}" var="c">
                        {!c.FirstName} {!c.LastName} <br/>
                    </apex:repeat>
                </apex:column>
            </apex:pageblocktable>
        </apex:pageBlock>
    </apex:form>
</apex:page>


Output:

User-added image

I hope this helps!

Best regards,
​Martijn Schwärzer

All Answers

Martijn SchwarzerMartijn Schwarzer
Hi Nagarjuna Reddy Nandireddy,

Please find below an example of how to accomplish this.

I've included 2 ways to do this:
  1. Using a single query
  2. Using a wrapper class
My controller (AccountContactWrapperController.class):
public class AccountContactWrapperController { 

	public List<Account> accounts{get;set;}
	public List<ACWrapper> ACWrappers{get;set;}

	public AccountContactWrapperController(){
		this.ACWrappers = new List<ACWrapper>();
		this.accounts = new List<Account>();

		//Get Accounts & Related Contacts in 1 query
		List<Account> accs = [Select Id, Name, (Select Id, FirstName, LastName From Contacts) From Account Limit 1];
		this.accounts = accs;

		//Get Accounts & Contacts in separate queries for Wrapper
		List<Account> accsForWrapper = [Select Id, Name From Account Limit 1];
		List<Contact> consForWrapper;
		Account acc;
		if(accsForWrapper != null && !accsForWrapper.isEmpty()){
			acc = accsForWrapper.get(0);
			consForWrapper = [Select Id, FirstName, LastName From Contact Where AccountId =:acc.Id];
		}
		if(consForWrapper != null){
			ACWrapper wrapper = new ACWrapper(acc, consForWrapper);
			this.ACWrappers.add(wrapper);
		}
	}

	public class ACWrapper{
		public Account account{get;set;}
		public List<Contact> contacts{get;set;}

		public ACWrapper(Account acc, List<Contact> cons){
			account = acc;
			contacts = cons;
		}
	}

}

My VF page (AccountContactWrapperTest.page):
<apex:page controller="AccountContactWrapperController" >
    <apex:form >

        <!--Display Account & Contact info from 1 Query-->
        <apex:pageBlock title="Info From Query">
            <apex:pageBlockTable value="{!accounts}" var="a">
                <apex:Column value="{!a.Name}" />
                <apex:Column headerValue="Contact Names">
                    <apex:repeat value="{!a.Contacts}" var="c">
                        {!c.FirstName} {!c.LastName} <br />
                    </apex:repeat>
                </apex:Column>
            </apex:pageBlockTable>
        </apex:pageBlock>

        <!--Display Account & Contact info from 1 Wrapper class-->
        <apex:pageBlock title="Info From Wrapper class">
            <apex:pageblocktable value="{!ACWrappers}" var="w">
                <apex:Column value="{!w.account.Name}" />
                <apex:column headervalue="Contact Names">
                    <apex:repeat value="{!w.contacts}" var="c">
                        {!c.FirstName} {!c.LastName} <br/>
                    </apex:repeat>
                </apex:column>
            </apex:pageblocktable>
        </apex:pageBlock>
    </apex:form>
</apex:page>


Output:

User-added image

I hope this helps!

Best regards,
​Martijn Schwärzer
This was selected as the best answer
Nagarjuna Reddy NandireddyNagarjuna Reddy Nandireddy

Hi Martijn Schwarzer
thank you so much it was helped me a lot
Martijn SchwarzerMartijn Schwarzer
Hi Nagarjuna Reddy Nandireddy,

Glad to be able to help!

If my answer helps you to solve your problem please mark it as best answer. It will help other to find best answer.

Regards,
Martijn
Ashley Sisti 11Ashley Sisti 11
@Martijn hoping you can help me - how would I do this if I had more than one record in the first object? So I'd like to be able to list one object, and the corresponding object linked via an Id, as one row. Basically need to get all the records in the first object, and then FOR each of them, get the matching record out of the second object. And then display fields from each in one row...?
Sheslie SenatSheslie Senat
@Ashley Sisti 11 Did you find a solution to your issue.  I'm having the same problem with one record showing when I'd like all of them to show.  Please let me know. 
farukh sk hdfarukh sk hd
This will help you for sure,
Example to display two object data using wrapper class which do not have relationship in between them,

https://www.sfdc-lightning.com/2018/10/wrapper-class-in-salesforce.html