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
Greg HGreg H 

Controller To Pass Data From More Than One Object?

I have an existing Visualforce page with a custom controller. When the code was originally written all of the data for display on the VF page was available via relationships and it was simple to gather in a single query. Business requirements have now changed and I need to get data from another object based on results from the original query. How do I combine two different query results into a single List?

 

My over-simplified, original code is below:

--- Visualforce Page --- <apex:page controller="cController"> <apex:pageBlockTable value="{!Data}" var="d"> <apex:column>{!d.Field_1__c}</apex:column> <apex:column>{!d.Field_2__c}</apex:column> </apex:pageBlockTable> </apex:page> --- Custom Controller --- public class cController { public Custom__c[] co = new List<Custom__c>(); public cController() { try { co = [SELECT Account__c, Field_1__c, Field_2__c FROM Custom__c]; } catch (Exception e) { //System.Debug('Exception: '+e); } } public Custom__c[] getData() { return co; } }

For lack of a better way to explain what I would like to do - here is some incorrect code:

 

public class cController { public sObject[] co = new List<sObject>(); public cController() { try { for (Custom__c temp : [SELECT Account__c, Field_1__c, Field_2__c FROM Custom__c]) { Contact cont = [SELECT Name FROM Contact WHERE AccountId = :temp.Account__c LIMIT 1]; co.add(temp, cont); } } catch (Exception e) { //System.Debug('Exception: '+e); } } public sObject[] getData() { return co; } }

 

Basically, I simply want the controller to pass back data from more than one object so I can display it together on the page. I thought I could use the generic sObject but that is proving difficult and I am not familiar with nested Lists (if that is even an option).

 

Any assistance would be greatly appreciated,

-greg

ryan_marplesryan_marples
One option is to define your own class and return that from your controller. You don't have to return SObjects, you can return any Apex class instance. Within that class you could have a List field that creates a hierarchical relationship. Something like this:

public class MyCustom {

public String field1 { get; set; }

public String field2 { get; set; }

public List otherItems { get; set; }

 

class MyOtherClass {

public String field3 { get; set; }

public String field4 { get; set; }

}

}

 



So then within your controller you would make the queries and fill in that collection.
Message Edited by ryan_marples on 03-20-2009 06:15 PM
Message Edited by ryan_marples on 03-20-2009 06:16 PM
aebenaeben

You need to use a wrapper.

 

class AccountWrapper //this can be a inner class in your controller

{

public Id accountId {get; set;}

public String name {get; set;}

 

public List<ContactWrapper> contacts {get; set;} 

 

//You don't need this class if the account-contact relationship is one-to-one.

class ContactWrapper //you can even have this class as a inner class in AccountWrapper

{

//declar the fields you want from contact 

 

 

In your controller

 

public List<AccountWrapper> data {get; set;}

 

Note in your VF page, when you display the contacts you need to use nested table. Hope it helps.