+ Start a Discussion
Santi Ram RaiSanti Ram Rai 

Controller extension with Controller not with StandardController.

Hello Everybody,
How to create Visualforve of Controller extension with Custom Controller not with StandardController.

Thanks.
Best Answer chosen by Santi Ram Rai
Abhishek_DEOAbhishek_DEO
As mentioned above, You need to pass standard controller/Custom to extension class so that it can access the data and methods. 

You may refer SF doc for further information
https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_controller_extension.htm

All Answers

Abhishek_DEOAbhishek_DEO

VF page : 


<apex:page controller="myController" extensions="myControllerext"/>

</apex:page>

Controller extension : 
public class myControllerext {
myController customController = null;
public myControllerExtension(myController customController) // "myController" is a custom controller
{
this.customController = customController;
}
}

As per doc: A controller extension is any Apex class containing a constructor that takes a single argument of typeApexPages.StandardController or CustomControllerName, where CustomControllerName is the name of a custom controller you want to extend.

https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_controller_extension.htm

AshlekhAshlekh
Hi,

Just create a Conroller.
 
Class ControllerApex
{
   public ControllerApex(){}
}
Just Create Extenstion.
 
Class ExtenstionApex
{
   public ExtenstionApex (ControllerApex CA){
   }
}

Create VFP
<apex:page  controller="ControllerApex" extensions="ExtenstionApex"> </apex:page>

Hope it helps you.

-Thanks
Ashlekh Gera
 
Santi Ram RaiSanti Ram Rai
Here is my ControllerApex Class:
public class ControllerApex {
	public ControllerApex(){}
    public String getFoo2() {
        return 'Hi' + ' ' + 'foo-Two';
    }
}

Here is my ExtensionApex Class:
public class ExtenstionApex {
	public ExtenstionApex (ControllerApex CA){}
    public String getFoo2() {
        return 'Hi' + ' ' + 'foo-Two';
    }
}

And here is my Vusialforce page:
<apex:page controller="ControllerApex" extensions="ExtenstionApex"> 
    <apex:outputText value="{!foo1}" />
    <apex:outputText value="{!foo2}" />
</apex:page>

But, i am getting this error message "Unknown property 'ControllerApex.foo1'".
Abhishek_DEOAbhishek_DEO

I could not see any property with name "foo1" in your controller or in your extension. 

Please create one in the same way you did for foo2

Santi Ram RaiSanti Ram Rai
How to create the property with name "foo1"?
 
Abhishek_DEOAbhishek_DEO
Eg.

public class ControllerApex {
    public ControllerApex(){}
    public String getFoo1() {
        return 'Hi' + ' ' + 'foo-one';
    }
    
        public String getFoo2() {
        return 'Hi' + ' ' + 'foo-two';
    }
}
Santi Ram RaiSanti Ram Rai
Actually, getFoo1 and  getFoo2 are two different methods in two different classes.
 
Abhishek_DEOAbhishek_DEO
You have to declare getFoo1 either in controller or in extension otherwise complier will report the error.
Santi Ram RaiSanti Ram Rai
Okey, i see. Thanks.
Santi Ram RaiSanti Ram Rai
Can you explain me why "ControllerApex CA" is here public ExtenstionApex(ControllerApex CA){}? And what it does? And how it works?
Abhishek_DEOAbhishek_DEO
As mentioned above, You need to pass standard controller/Custom to extension class so that it can access the data and methods. 

You may refer SF doc for further information
https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_controller_extension.htm
This was selected as the best answer