Activity Forums Salesforce® Discussions Can any one tell me when do we use custom controller in Salesforce with extension class?

  • Radhakrishna

    Member
    August 21, 2017 at 3:31 am

    Hello Uday,

    Hope you will have better understanding now with these many answers... 🙂

    Let me add few more points here, I would say, A Controller is an Apex Class but an Apex Class is not always a Controller. Further you can call apex class in many different ways:-

    From another class
    From Trigger
    From Visualforce page
    From Developer console
    Form JavaScript button, Links
    From Home page components

    Now if we talk about Controller:-
    In MVC, the view (the Visualforce page) interacts with a controller, and the controller provides functionality to the page. For example, the controller can contain the logic to be executed when a button is clicked. A controller also typically interacts with the model (the database)—making available data that the view might want to display, or pushing changes back to the database.

    Standard Controller:-
    Most standard and all custom objects have standard controllers that can be used to interact with the data associated with the object, so you don’t need to write the code for the controller yourself. You can extend the standard controllers to add new functionality, or create custom controllers from scratch.

    Custom controller:-
    When 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, Visualforce lets you take the reigns. You can write a custom controller using Apex and completely control your app’s logic from start to finish.

    Now I will give you a simple scenario:-
    Suppose you have been asked to display the list of all Account on a visual force page then you will definitely start with Standard controller and using pageBlock table you can simply display all accounts. Now if I say to display list of account having Industry = "Agriculture" and Type = "Customer - Channel", then you have to think about custom controller where you write SOQL with some filter on Account.

    So, you have a better control over the page now as you are writing the code (a custom controller) that will fetch the data based on your requirement.

    Hope this will help you..!!!

  • Avnish Yadav

    Member
    September 30, 2018 at 5:34 am

    Hello,

    When using custom controller you may or may not create constructor as a default constructor is always present when you ceate any class.
    Thanks.

Log In to reply.

Popular Salesforce Blogs

Popular Salesforce Videos