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
Del_SantosDel_Santos 

Multiple Standard Controller

Hi,

 

Is it possible to use multiple standard controller in a VF page..

 

i.e. something like this..

<apex:page standardController="Quote" standardController="Account"

 

 

 

Thanks,

Del

Best Answer chosen by Admin (Salesforce Developers) 
SteveBowerSteveBower

Pradeep, I believe you are incorrect.  As long as you are willing to do the initialization of the Standard Controllers object yourself instead of relying on the fields in the Page, this should be no problem.   Try this.

 

 

<apex:page standardController="Contact" extensions="Controller_trySomething">
	<apex:form >
	
		<apex:outputText rendered="false" value="{!Contact.AccountId}{!Contact.OwnerId}"/>
		
		My Contact's First Name is: <apex:inputField value="{!Contact.FirstName}"/>
		<br/><br/>
		My Contact's Account Name is <apex:inputField value="{!Account.Name}"/>
		<br/><br/>
		My Contact's Owner First Name is: <apex:inputField value="{!User.FirstName}"/>
		<br/><br/>
		<apex:CommandButton value="Save Contact" action="{!cancel}"/>
		<apex:CommandButton value="Save Account" action="{!myAccountController.save}"/>
		<apex:CommandButton value="Save User" action="{!myUserController.save}"/>
		<br/>
		<br/>
		<apex:CommandButton value="Save All" action="{!doSaveAll}"/>
		<br/>
		<br/>
		<apex:CommandButton value="Cancel to Contact" action="{!cancel}"/>
		<apex:CommandButton value="Cancel to Account" action="{!myAccountController.cancel}"/>
		<apex:CommandButton value="Cancel to User" action="{!myUserController.cancel}"/>
	</apex:form>
</apex:page>

 

public with sharing class Controller_trySomething {

	public ApexPages.StandardController myAccountController {get; set;}
	public Account Account {get; set;}
	
	public ApexPages.StandardController myUserController {get; set;}
	public User User {get; set;}
	
	public ApexPages.StandardController standardContactController;
		
	public Controller_trySomething(ApexPages.StandardController cntrl) {
		
		standardContactController = cntrl;
		
		Contact con = (Contact)cntrl.getRecord();
		Account = [select Name from Account where id = :con.AccountId][0];
		User = [select FirstName from User where id=:con.OwnerId][0];
		
		myAccountController = new ApexPages.StandardController(Account);
		myUserController = new ApexPages.StandardController(User);
	}
	public pageReference doSaveAll() {
		standardContactController.save();
		myAccountController.save();
		myUserController.save();
		return null;
	}
}

 

The key is that I'm instantiating a local reference to the Account object (for example), and I'm *also* using that reference in another instantiation of a standard controller for that object.  So, that gives me access to both the fields of the record using <apex:inputfield>, etc. and access to the Controller methods for that record.

 

So, I think this yields the behavior of multiple standard controllers within one VF page.  

 

They key thing that's missing is that I need to load the Account records fields myself instead of having the VF mechanism automatically see that I'm using Account.Firstname, and automatically loading it for me.  But,  I don't see that as a huge distinction since it's declarative regardless of if it's in the Page or the Class.

 

Best, Steve.

 

p.s. My apologies for the hacked up code, I was just trying to demo it.

 

 

 

 

All Answers

Imran MohammedImran Mohammed

There cannot be more than one standard controller in a VF page.

But can have multiple extension controllers for a page.

 

One way what i can suggest for the requirement you have is establish relationship in between the two objects, which will help you traversing to the data of another object.

SteveBowerSteveBower

Note that while you can only have one standard controller *defined on the <apex:page> tag*, you can have as many as you want inside your own Apex code.

 

So, if you declare:

<apex:page standardController="Contact" extension="myController">

 

and in myController do:

 

public with sharing class myController {

 

ApexPages.StandardController myAccountController;

ApexPages.StandardController myUserController;

ApexPages.StandardSetController> myOpportunitiesSetController;

 

ApexPages.StandardController myContactController;

 

myController(ApexPages.StandardController stdCntrl) {

myContactController = stdCntrl;

 

myAccountController = new ApexPages.StandardController([select id, Name from Account where id = :myContactController.getRecord().Account]);

myOpportunitiesSetController = new ApexPages.StandardSetController([select id, name, Amount, from Opportunity where AccountId = :myAccountController.getId()]);

//etc.

}

 

 

In short, you can use the ApexPages classes yourself for access to their methods.

 

 

Note that you could also have the illusion of multiple controllers for the same page by using Components behind the scenes.

 

Best, Steve.

 

 

 

 

Imran MohammedImran Mohammed

Thanks steve for sharing valuaable information.

Pradeep_NavatarPradeep_Navatar

No, It is not possible to use multiple standard controllers in a VF page. We can inherit one standard controller at a time otherwise it will give error because we generally pass a record id of the Standard Object in URL to get all the details of that record on the VF page. You can inherit multiple custom controllers in a VF page using extensions attribute in apex page tag.

SteveBowerSteveBower

Pradeep, I believe you are incorrect.  As long as you are willing to do the initialization of the Standard Controllers object yourself instead of relying on the fields in the Page, this should be no problem.   Try this.

 

 

<apex:page standardController="Contact" extensions="Controller_trySomething">
	<apex:form >
	
		<apex:outputText rendered="false" value="{!Contact.AccountId}{!Contact.OwnerId}"/>
		
		My Contact's First Name is: <apex:inputField value="{!Contact.FirstName}"/>
		<br/><br/>
		My Contact's Account Name is <apex:inputField value="{!Account.Name}"/>
		<br/><br/>
		My Contact's Owner First Name is: <apex:inputField value="{!User.FirstName}"/>
		<br/><br/>
		<apex:CommandButton value="Save Contact" action="{!cancel}"/>
		<apex:CommandButton value="Save Account" action="{!myAccountController.save}"/>
		<apex:CommandButton value="Save User" action="{!myUserController.save}"/>
		<br/>
		<br/>
		<apex:CommandButton value="Save All" action="{!doSaveAll}"/>
		<br/>
		<br/>
		<apex:CommandButton value="Cancel to Contact" action="{!cancel}"/>
		<apex:CommandButton value="Cancel to Account" action="{!myAccountController.cancel}"/>
		<apex:CommandButton value="Cancel to User" action="{!myUserController.cancel}"/>
	</apex:form>
</apex:page>

 

public with sharing class Controller_trySomething {

	public ApexPages.StandardController myAccountController {get; set;}
	public Account Account {get; set;}
	
	public ApexPages.StandardController myUserController {get; set;}
	public User User {get; set;}
	
	public ApexPages.StandardController standardContactController;
		
	public Controller_trySomething(ApexPages.StandardController cntrl) {
		
		standardContactController = cntrl;
		
		Contact con = (Contact)cntrl.getRecord();
		Account = [select Name from Account where id = :con.AccountId][0];
		User = [select FirstName from User where id=:con.OwnerId][0];
		
		myAccountController = new ApexPages.StandardController(Account);
		myUserController = new ApexPages.StandardController(User);
	}
	public pageReference doSaveAll() {
		standardContactController.save();
		myAccountController.save();
		myUserController.save();
		return null;
	}
}

 

The key is that I'm instantiating a local reference to the Account object (for example), and I'm *also* using that reference in another instantiation of a standard controller for that object.  So, that gives me access to both the fields of the record using <apex:inputfield>, etc. and access to the Controller methods for that record.

 

So, I think this yields the behavior of multiple standard controllers within one VF page.  

 

They key thing that's missing is that I need to load the Account records fields myself instead of having the VF mechanism automatically see that I'm using Account.Firstname, and automatically loading it for me.  But,  I don't see that as a huge distinction since it's declarative regardless of if it's in the Page or the Class.

 

Best, Steve.

 

p.s. My apologies for the hacked up code, I was just trying to demo it.

 

 

 

 

This was selected as the best answer
Del_SantosDel_Santos

Thanks to all..:))

GeniuanGeniuan
Thanks @[SteveBower], excelent answer, clear and to the point.
riazriaz
it ok, now i am sure . we cannot  add two standard controller
GeniuanGeniuan
Thank you. Now I have VF pages with fields from Accounts, Opportunitys, Contacts and Custom objects. Nice.
mohammed ali 7mohammed ali 7
Hi ,
   I want to use in one VF page two standard controller like account and contact , but i don't want to use any extension , because it a a custom class again , i want to use only standard controller ...? 
Any one have such mind to solve this