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
morleymorley 

Multiple controllers?

Hi there ... I'm trying to create a VisualForce page with Accounts and Contacts. The page will show a limited list of Accounts at the top, based on a SOQL query like "Select Name, Phone, From Account Where (ShippingCountry = 'USA') ". When the user clicks on one Account from the list, I want to show a filtered list of its Contacts below, again based on a SOQL query like "Select Name, Email From Contact Where (Title = 'Director' AND AccountId ={SelectedAccount}) ".

Is this possible, given that the documentation clearly states that "A page can only reference one controller at a time."?

Thanks for any help.



Regards,

Glenn.
jpizzalajpizzala
You may query any object you like regardless of the controller. However, if you are looking for native object functionality for both Accounts and Contacts, you will have to implement that yourself (for now) in a custom controller along with your other custom methods.

In the Winter '08 release, you will be able to create controller extensions so you can add custom functionality without interfering with the native.

Hope this helps!
morleymorley
Thanks for the reply. I'm happy to write the custom controller, but I don't quite understand what you mention about " You may query any object you like regardless of the controller". I thought you defined one controller for the page (be it a standard or custom controller) and that determined the records available on that page?
jpizzalajpizzala
The standardController will allow for you to use the native functionality of the specified object (for instance 'Account' if you use the Account standardController). This native functionality, among other things, will allow you to use buttons like 'Save' without coding the process yourself; or grab all values of the 'Industry' picklist (on Account) with the <apex:inputField> tag. Since you specified the object you would like to use as the standardController, Salesforce will treat your Visualforce page as if it were an Account object.

Since you need to do some custom queries, you won't be able to use a standardController anyway (that is, until extensions are released with Winter...). Here is what you can do to grab information from multiple objects in your custom controller:

Code:
public class myController {
  public List<System.Account> getAccounts() {
    return [select id, name, description from account];
  }

  public List<System.Contact> getContacts() {
    return [select id, name, email from contact];
  }
}

Now that I'm reading over what I just wrote, I realize it may not be 100% clear, so if you need any more clarification, please let me know.

Regards

morleymorley
Actually, that looks like it might be just what I need. I used the controller you defined and was easily able to build a page that rendered a list of Accounts and Contacts. I now just have to filter the list of Contacts when the user clicks on a specific Account.

Thanks!
RickyGRickyG
Just in case it is not clear enough from the example, you can only reference one controller on a Visualforce page, but a custom controller can interact with multiple objects through your code implementation.

Hope this helps.