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
SKT CloudComputingSKT CloudComputing 

Controller and Apex class?

1- Diff. between controller and Apex class?

2- any specific reason to use custome controller?

Can anyone help to understand these two concepts?

naga raju 19naga raju 19
In the context of visualforce, to take advantage of the built-in features ofVisualforce to access your org's data, you must use a controller. All controllers are Apex classes, either standard Apex classes (ApexPages.StandardController or ApexPages.StandardSetController  like Account,Lead) or your own custom Apex class(custom controller like Acc__C). Custom Controller Should containe__C...
SKT CloudComputingSKT CloudComputing
So, it means there is no such difference between controller and apex class.

If we use controller and extensions in VF page ,then we must need to write  respective apex classes.

Is it correct?

Temoc MunozTemoc Munoz
I think you're mixing concepts here. Controller and Class are different levels of abstraction.

A controller is a component in the Model-View-Controller Architecture that communicates with both the Model and View.

A class is a set of fields and operations only available in Object-Oriented Programming Languages. A class can be the realization of a controller, a model, a wrapper class, a test factory class, etc.

In a normal software development cycle, you would normally define the controller at the design phase. The class would be defined in the development phase.

"If we use controller and extensions in VF page ,then we must need to write  respective apex classes."
Yes, this is correct.

SKT CloudComputingSKT CloudComputing
Thanks Temoc. I am new to salesforce.

What I am trying to understand is, if we  put controller='ABC", then we must write respective apex class.

These two differ at the level they are being used. At design level it's known as "Controller" but in actual at development time it's class writeen in apex laguage.

 Did I understand it correctly?

Hi  SKT,

Basically Standard controller,Controller  or extensions are all apex classes . In  <apex:page> parent component we have StandardController ,Controler and extensions where we need to add some thing with quote .
Let me explain on eby one which will help you .

StandardController :- Basically it is a object api name we need to assign here .why because when ever you will create a object or any standard   object salesforce has created internally most of the object have a class related to this with name as the object name .So when you will say standard controller it wil refer to that class which has always 9(nine) methods in the class .
To more detail about that you can check below links !! (http://apex_pages_standardcontroller)

After this if you want any custom funcationally you need to use the extensions attribute where you can assign your custom code .
This will be always your apex class name developed by you .

If standard controller i snot there then you need to use controller and then you assign the extensions attribute where both class will be your custom apex class  .

In one case you can not assign standard controller and controller attribute .
Case one -When standardController is there and you are writing an extension then you need to create a one argument constructor like below 

Public MyCustomClass(ApexPages.StandardController stdController)
Same way if you have controller and you are adding extensions then you need to add a constuctor like below
Public MyCustomClass(MyCustomController  cstController)
Any way when ever you will save from development footer system wil ask you to create this by clicking on the link in red color .

I think you are clear now let me know if you have any doubt still 
Rajneesh Ranjan 23Rajneesh Ranjan 23


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..!!!
Rohit Shetty 6Rohit Shetty 6
Hi, i have a similar issue, not able to save the following controlled code as an apex class - please help

public void approveActionFunction(){    public void approveActionFunction(){List<Employee_Detail__c> selectedListViewRecords = (List<Employee_Detail__c>) standardSetController.getSelected();
                try{            String idkey = UserInfo.getUserId();
            Boolean hasAccess = false;
            List<Profile> p = [SELECT Id, Name FROM Profile WHERE Name='System Administrator'];
            if(UserInfo.getProfileId() == p[0].id)                hasAccess = true;                List<Employee_Detail__c> recordList = new List<Employee_Detail__c>(TFFs[0]);                List<String> ids = new List<String>();                for(Employee_Detail__c rec: selectedListViewRecords){                    ids.add(;                }                system.debug('ids'+ids);
                List<Approval.ProcessWorkitemRequest> requests = new List<Approval.ProcessWorkitemRequest>();
                List<ProcessInstanceWorkitem> workItems;
                if(hasAccess)                    workItems = [SELECT Id, ProcessInstanceId FROM ProcessInstanceWorkitem WHERE ProcessInstance.TargetObjectId IN :ids ];
                else                     workItems = [SELECT Id, ProcessInstanceId FROM ProcessInstanceWorkitem WHERE ActorId = :idkey AND ProcessInstance.TargetObjectId IN :ids ];
                            for(ProcessInstanceWorkitem workItem : workItems){                         Approval.ProcessWorkitemRequest req = new Approval.ProcessWorkitemRequest();                  
                                     req.setAction('Approve');                  req.setComments('Auto approved.');                  requests.add(req);                }                Approval.ProcessResult[] processResults = Approval.process(requests);         } catch ( Exception ex ){ //return null; system.debug(ex.getMessage());        }              }