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
UmapenUmapen 

Standard controller and custom controller in one apex page

I have a custom controller and apex page associated with it. I am posting the page with button click in account page layout. When my apex page is displayed I want to display some values from account object like name, city. my customer controller with all the logic in it and works fine.

 

1. My page starts with the line 

  <apex:page Controller="MyCustomSelController">

  Can I add StandardController = "Account" to get the account fields?

2. How do I use standardController and pass the values to my custom controller?

 

Thanks

Uma

 

Best Answer chosen by Admin (Salesforce Developers) 
gtuerkgtuerk

You should have a controller variable in your custom controller class and you should set that member variable to the inbound controller.  like so:

 

public class MyCustomController{

 

 private final ApexPages.StandardController controller;

public MyCustomController(ApexPages.StandardController controller){

  this.controller = controller;

}

}

 

Anything you wish to initialize you'll need to add to the constructor that the vf page uses (it always uses the one with a controller, whether a standardcontroller or standardsetcontroller)

All Answers

gtuerkgtuerk

Use your Custom controller as an extension

 

<apex:page standardController = "Account" extensions="myCustomController">

</apex:page>

UmapenUmapen

Thanks for the quick response.

do I have to modify my controller to add another contructor? I am getting the following error?

 

Error: Unknown constructor 'MyCustomSelController.MyCustomSelController(ApexPages.StandardController controller)'

do I have to change any code, besides adding a contructor?

 

 

gtuerkgtuerk

You should have a controller variable in your custom controller class and you should set that member variable to the inbound controller.  like so:

 

public class MyCustomController{

 

 private final ApexPages.StandardController controller;

public MyCustomController(ApexPages.StandardController controller){

  this.controller = controller;

}

}

 

Anything you wish to initialize you'll need to add to the constructor that the vf page uses (it always uses the one with a controller, whether a standardcontroller or standardsetcontroller)

This was selected as the best answer
UmapenUmapen

Thanks for the quick response. Here is the final code that worked.

public class MyCustomController{
   public MyCustomController() {}
 private final ApexPages.StandardController controller;

public MyCustomController(ApexPages.StandardController controller){

  this.stdAcctController = (Account)stdcontroller.getRecord();
}

}

 

Thank you so much.