3

When and how to decide whether to go with Standard Controller OR Custom Controller?

I have created so many vf pages,classes, extensions but still I'm not sure to include standard controller or custom controller in vf page.

Can anybody please tell me practically (not theoretically)

3

Rather than paraphrasing the difference, here is a great explanation, and I have have also added my own view.

A standard controller consists of the same functionality and logic that is used for a standard Salesforce 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. If you use a standard controller on a page and the user doesn’t have access to the object, the page will display an insufficient privileges error message. You can avoid this by checking the user’s accessibility for an object and displaying components appropriately.

A custom controller is a class written in Apex that implements all of a page’s logic, without leveraging a standard controller. If you use a custom controller, you can define new navigation elements or behaviors, but you must also reimplement any functionality that was already provided in a standard controller. Like other Apex classes, custom controllers execute entirely in system mode, in which the object and field-level permissions of the current user are ignored. You can specify whether a user can execute methods in a custom controller based on the user’s profile.

See VisualForce in Practice for a complete guide.

Using a standard controller means that you will not have to write any tests, or maintain any code - that is the main benefit, but you are restricted to more basic functionality and logic, but this is fine if you just want a custom visualforce page for Create, Update and Delete. However, as mention in VisualForce in Practice you could use a standard controller for all of these features, then extend it with a custom controller if you wanted to do something more fancy - like a complicated calculation or have two or more objects within the same visualforce page.

| improve this answer | |
  • 1
    Ok. But What I feel is, if I can use standard controller with extension then what is purpose of using custom controller. – SfdcPro Dec 11 '15 at 9:38
  • Remember your Visualforce page may not be tied to a specific object, you can write a visualforce page that does not interact with objects or perform DML. For example you might want a VF page that interacts with an external API. – developer__c Dec 11 '15 at 9:40
1

Simple reason is,

If you need to include the visualforce page in the standard layout, you have to use standard controller

Practical example: In the related list of Opportunity you have line items/ products associated. The users want to be able to sort them dynamically and also find certain record like in this js, https://www.datatables.net , The only way is to build the page with opportunity standard controller and add that inline of standard pagelayout.

In the same line, if the users want to have a tab, visualforce tab, you have to create custom controller.

We can still go on with lots of explanations, but you have to make your own understandings by experience.

HTH Prabhan

| improve this answer | |
0

Practically, if the server-side functionality of your page uses what a standard controller provides, you would be best off using that rather than re-inventing the wheel. If you can tell that the functionality is mostly similar, you could always start with the Standard Controller and add extensions when you need to add additional functionality.

Another use for the Standard Controller, even if you aren't actually using the functionality, would be if you want to add the Visualforce Page to a button or page layout. Adding a Visualforce page to a button or page layout requires you to declare the corresponding standard (list) controller.

Also if you find yourself writing a Visualforce page for a Group/Professional org, you won't be able to use Apex, so you will have to use the Standard Controller.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.