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
Vijay sidaraddiVijay sidaraddi 

If i have two apex classes

Hi Team,

Can someone tell me how to identify/rectify if i have two classes one is custom controller and other one is controller extension on what base we can decide its custom controller and controller extension..

Thanks
Vijay S
Best Answer chosen by Vijay sidaraddi
RAM AnisettiRAM Anisetti
Hi Vijay,

The extension class required to implement a constructor that accepts the type of the controller(ApexPages.StandardController). You will then typically want to get a handle to the standard controller instance that is floating around, and the sObject instance that it represents.

i.e,

The extension structure is like this
 
public with sharing class MyExtClass {

 
  public Account acct {get;set;}
  public MyExtClass(ApexPages.StandardController ctrl ){
     
      acct = (Account) ctrl.getRecord();

  }

 

All Answers

RAM AnisettiRAM Anisetti
Hi Vijay,

The extension class required to implement a constructor that accepts the type of the controller(ApexPages.StandardController). You will then typically want to get a handle to the standard controller instance that is floating around, and the sObject instance that it represents.

i.e,

The extension structure is like this
 
public with sharing class MyExtClass {

 
  public Account acct {get;set;}
  public MyExtClass(ApexPages.StandardController ctrl ){
     
      acct = (Account) ctrl.getRecord();

  }

 
This was selected as the best answer
ManojjenaManojjena
Hi Vijay ,

What Ram told it is almost correct .This example he has given if you have standard controller and you have added extension to that .

As your controller is standard controller your concructor should contain the argument of standard controller .
and the format will be  as belwo always
ControlerCls(ApexPages.StandardController ctrl )

Same way if you have controller as a custom  class and extension is also a custom apex class .Then the extension should contain an argument of the controller class in constructor like below.
ExtensionCls(ControlerCls contl)
So now concept is your controller should not have any constuctor with argument of any class .
However your extension always have one argument constructor of your controller class .
I think you are clear now .
Let us know if it helps !!
Thanks
Manoj
 
Amit Chaudhary 8Amit Chaudhary 8
Custom Controller:
1) It is an Apex class that implements all of the logic for a page without leveraging a standard controller.
2) You can use only one Apex class.
3) You can Use custom controllers when you want your Visualforce page to run entirely in system mode, which does not enforce the permissions and field-level security of the current user.

Extension:
1) A controller extension is any Apex class containing a constructor that takes a single argument of type ApexPages
2) It provides additional functionality to a controller – either a standard controller or a custom controller.
3) You can use multiple Apex classes separated by comma.
4) Use controller extensions when:You want to leverage the built-in functionality of a standard controller but override one or more actions, such as edit, view, save, or delete.You want to add new actions.You want to build a Visualforce page that respects user permissions. Although a controller extension class executes in system mode, if a controller extension extends a standard controller, the logic from the standard controller does not execute in system mode. Instead, it executes in user mode, in which permissions, field-level security, and sharing rules of the current user apply

User-added image

Please check below post for more detail
http://www.salesforcetutorial.com/custom-controllers-controller-extensions/


Please let us know if this will help you