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
bs881026bs881026 

why custom extension with custom controller

Why do we need a custom extension when we are using a custom controller .
Please help me on this.

Thanks.
Best Answer chosen by bs881026
Jancy MaryJancy Mary
Hi Guys,

I got same question too while I was learning Extensions, why Extensions? How it is different from Controllers, apart from syntax

Well I was able to find the below two reasons to use Extensions. In fact Controllers & Extensions are for same purpose which I understood.
1. Suppose there exists a VF page which has a custom controller called on it, and it is well tested and currently in use by the end users, if you want to add additional functionality on this VF page, you better don't disturb the existing program which would hit your business big time in case if anything goes wrong, rather write an extension and use it on VF Page.

2. We can't call multiple custom controllers on a VF Page, but we can call second controller while we have the first controller on VF page, and however you need to define your second controller as Extension rather as controller. 

For more details go through this audio class, 
https://www.youtube.com/watch?v=uMeXBlTEknI


Note: We write multiple controller or extensions rather than defining all in one huge class, this is to maintain the code and make it easy to reuse wherever it is necessary.

Thanks,
 

All Answers

Jha dilipJha dilip
Standard controllers can provide all the functionality you need for a Visualforce page because they include the same logic that is used for a standard page. For example, if you use the standard Accounts controller, clicking a Save button in a Visualforce page results in the same behavior as clicking Save on a standard Account edit page.
However, if you want to override existing functionality, customize the navigation through an application, use callouts or Web services, or if you need finer control for how information is accessed for your page, you can write a custom controller or a controller extension using Apex:

if you want to use standard salesforce functionality and also want to add some extra features which standard one does not provide then you can use extension with that.

but in case of custom controller it is totally a fresh one if you don't want to use standard functionality and need to develop everything from scrath you can go for custom controller
bs881026bs881026
Hi, my question was that, what is the purpose of 'Extensions'  along with Custom Controller.
If we can accomplish everything custom in custom controller, then what is the purpose of using the extensions walong with it.

Thanks.
Jha dilipJha dilip
Extension controller. with a custom controller (this is often overlooked and is a way to provide common functionality across a number of pages).
Rupali PophaliyaRupali Pophaliya
Custom controller - Implements all of the logic for a page without leveraging a standard controller. 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.

A controller extension - Extends the functionality of a standard or custom controller. 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.
Jancy MaryJancy Mary
Hi Guys,

I got same question too while I was learning Extensions, why Extensions? How it is different from Controllers, apart from syntax

Well I was able to find the below two reasons to use Extensions. In fact Controllers & Extensions are for same purpose which I understood.
1. Suppose there exists a VF page which has a custom controller called on it, and it is well tested and currently in use by the end users, if you want to add additional functionality on this VF page, you better don't disturb the existing program which would hit your business big time in case if anything goes wrong, rather write an extension and use it on VF Page.

2. We can't call multiple custom controllers on a VF Page, but we can call second controller while we have the first controller on VF page, and however you need to define your second controller as Extension rather as controller. 

For more details go through this audio class, 
https://www.youtube.com/watch?v=uMeXBlTEknI


Note: We write multiple controller or extensions rather than defining all in one huge class, this is to maintain the code and make it easy to reuse wherever it is necessary.

Thanks,
 
This was selected as the best answer
Jancy MaryJancy Mary
And I would like to mention that all points what Mr. Dilip posted seems absolutely valid strong points, they are in fact right bullet points for "What Controllers and Extensions does.
bs881026bs881026
Thanks Jancy and all others as well.