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.
2 Answers
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'. Force.com 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
}
}
-
Hi Steven, how would you pass record ID parameters to such controller, i.e. from a multi-select button in a related list?– dzhCommented Feb 13, 2014 at 16:44
-
I'd probably just set them as url parameters and issue a GET if its a list of ids. Commented Feb 17, 2014 at 10:55
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="{!acc.object_account_1.name}" />
</apex:pageblocktable>
<apex:commandButton value="save"/>
<br/>
<br/>
<br/>
<apex:pageblocktable value="{!contactwrapperret}" var="con">
<apex:column value="{!con.object_contact_2.name}" />
</apex:pageblocktable>
<apex:commandButton value="save"/>
</apex:pageblock>
</apex:form>
</apex:page>
Controller:
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);}
}
}
-
This seems overcomplicated. What exactly is the point of the wrapperclass? If both sObject types are handled seperatley anyways why wrap them? Commented Sep 24, 2012 at 0:25
-
True!!! My intent was to demonstrate the actual way to wrap multiple objects. I do understand wrapper works only when there is a need for a Sobject and a related object to be wrapped in the same label. I was trying to wrap 2 different objects in the same wrapper.– RaoCommented Sep 24, 2012 at 17:05