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
thinhtvuthinhtvu 

Creating an Apex controller extension that allows for multiple standard controllers in a VF page

Hello,

 

I'm trying to create an Apex controller extension that allows me to use more than one Standard controller in a VF page. Right now, I'm creating a Document Request Form for my organization, but one of the steps requires me to retrieve fields from Contacts, Opportunities, and Accounts. I was wondering if there was a way to write an extension to incorporate the functions of these standard controllers into one controller?

 

I tried doing this, while still using the Opportunity standard controller in my VF page, and this class as an extension, but it didn't work the way I thought it would:

 

    private final Opportunity opp;
    private final Contact cont;
    
    public ApexPages.StandardController stdCtrl;
    public drfExtension(ApexPages.StandardController std)
    {
        stdCtrl = std;
        
        this.opp = (Opportunity)std.getRecord();
        this.cont = (Contact)std.getRecord();
    }

 

Andy BoettcherAndy Boettcher

Quick and dirty method if you have a limited number of sObjects you're testing for - you can use getDescribe...?

 

sObject objGeneral = std.getRecord();
String strTypeName = objGeneral.getSObjectType().getDescribe().getName();

if(strTypeName == 'Contact') {
     this.cont = (Contact)std.getRecord();
}

if(strTypeName == 'Opportunity') {
     this.opp = (Opportunity)std.getRecord();
}

 

Like I said - quick and DIRTY, but it should work for ya.

 

-Andy

thinhtvuthinhtvu

It didn't work. I thought it did at first, but that was only for the page that was dealing strictly with Opportunities. For the Contact and Account pages, I get the errors "Unknown property 'OpportunityStandardController.contact'" and "Unknown property 'OpportunityStandardController.account'"