Find your content:

Search form

You are here

Multiple custom objects in a single VisualForce page


How would I go about using two custom objects (unrelated to each other) in a single VF page? Would I set the first object as a standard controller and use an extension controller for the second, or make only one custom controller? Or a wrapper class? I want to be able to create records for each object in a single page - and make sure they each have separate save functions. I also want to be able to show all of their records on that same page. How do I do those things? Thanks in advance.

Attribution to: sharrie

Possible Suggestion/Solution #1

I would probably create a Custom Apex Controller with a reference to both objects and action methods to handle the two different save actions you wish to run, the main reason being that you wish to show lists of objects as well as create a single record. Using a standard controller + extension really just confuses things since you have a bunch of functionality you won't need specific to only a small part of the data you interacting with.

When you say 'show all of their records on the same page', you need to be more specific about 'all'. has many constraints around the size of lists, size of SOQL query result sets and the size of the view state so you are probably constrained to lists of 10 if you attack the problem naively by querying everything and storing it in a list.

If you want to handle large list sizes you need to look at Standard Set Controllers, if you are looking at a small list then you should expose a List.

Either way you can use or / to render the list.

So, naive code from the top of my head below:

public class with sharing MyCustom Controller {

 public List<CustomObj1__c> listOfCustomObjs1 {get; set;}
 public List<CustomObj2__c> listOfCustomObjs2 {get; set;}
 public CustomObj1__c customObj1 {get;set;}
 public CustomObj2__c customObj2 {get;set;}

public MyCustomController() {
    listOfCustomObjs1 = [Select id, name from CustomObjec1__c];

 public PageReference saveCustomObj1() {
   // save logic


Attribution to: Steven Herod

Possible Suggestion/Solution #2

How you would want to show the data also matters eg: I am using standard objects here. you can change the objects to custom as required If you want to show the data in 2 separate tables I would suggest wrapper class : simple and sweet Check the code below to see if this can be something useful/you want to do

<apex:page controller="wrapper_test_controller">
    <apex:form >
        <apex:pageblock >
         <apex:pageblocktable value="{!accountwrapperret}" var="acc">
          <apex:column value="{!}" />
          <apex:commandButton value="save"/>
          <apex:pageblocktable value="{!contactwrapperret}" var="con">
          <apex:column value="{!}" />
          <apex:commandButton value="save"/>


public with sharing class wrapper_test_controller {
public list<wrapperclass> wrapperelement_for_account{get;set;}
public list<wrapperclass> wrapperelement_for_contact{get;set;}
list<account> object1 = [select id,name from account limit 10];
list<contact> object2 = [select id,name from contact limit 10];

public list<wrapperclass> getaccountwrapperret(){
wrapperelement_for_account = new list<wrapperclass>();
for(account obj1: object1){
wrapperelement_for_account.add(new wrapperclass(obj1));
return wrapperelement_for_account;
public list<wrapperclass> getcontactwrapperret(){
wrapperelement_for_contact = new list<wrapperclass>();
for(contact obj2: object2){
wrapperelement_for_contact.add(new wrapperclass(obj2));
return wrapperelement_for_contact;
public class wrapperclass{
public account object_account_1{get;set;}
public contact object_contact_2{get;set;}

public wrapperclass(account obj1){
this.object_account_1 = (obj1);
public wrapperclass(contact obj2){
this.object_contact_2 = (obj2);}

Attribution to: Rao
This content is remixed from stackoverflow or stackexchange. Please visit

My Block Status

My Block Content