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
ShivaShiva 

How to use multiple controllers

Is it possible to use multiple controllers in the same page?

 

Any examples?

Paul OveryPaul Overy

You can load another controller as a class:

 

mySecondController cont2 = new mySecondController();

 

 This will allow you to make calls into it, but it won't get updates from a Visualforce Page, which is what I'm guessing you would like to do.

 

aebenaeben

You can do it with extension controllers.

 

For Example 

 

standardController="Contact" extensions="controller1, controller2"

 

 

BrianWKBrianWK
Aeben -very cool, I wasn't aware I could have more than one extension. That would've made so many of my projects easier to manage!
aebenaeben

Problem with multiple controllers is, a controller in the list will not know anything about the other controllers.

 

I would suggest using  one controller with helpers and do something like a delegate pattern.

qwidjiboqwidjibo

You could also set the separate controller for different components within a page, I tend to have the page controller build the master data structure and then hand it off to the component controllers (assignTo) to save on unecessary SOQL, using this delegate pattern makes it easiier to see abstract away detail and not have the master controller handle all the work.