Controllers in Salesforce

Controllers are broadly categorized into two types (Controller part of MVC architecture)

♦ Standard Controller
♦ Custom Controller

Standard Controller is a inbuilt controller used to associate the visualforce page with salesforce object. Using this we need not write business logic using apex to interact with page and the database. The standard methods such as “save”, “cancel”, “edit”, “delete” etc., can be accessed from the page and perform the corresponding actions without writing the logic. To implement this,  we need to learn few components and related attributes as below.

The page component has to include an attribute “standardController” to refer the required standard controller. The standard controller is always referred by corresponding object name. In below code, the page is associated with a standard controller “Account”. Now this page can be used to work with account object through various inbuilt methods and features of standard controller.

<apex:page standardController="Account">
    <apex:form >
        <apex:pageBlock title="My Content" mode="edit">
            <apex:pageBlockButtons >
                <apex:commandButton action="{!save}" value="Save"/>
            </apex:pageBlockButtons>
            <apex:pageBlockSection title="My Content Section" columns="2">
                <apex:inputField value="{!account.name}"/>
                <apex:inputField value="{!account.site}"/>
                <apex:inputField value="{!account.type}"/>
                <apex:inputField value="{!account.accountNumber}"/>
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>

The above page has other components too. Please check on this page to get the detailed explanation of a similar page creation and components. But to brief about this page, check the explanation for each component as below

<apex:pageBlock> – this brings look and feel for your page similar to a salesforce standard page layout

<apex:commandButton> – Brings a custom button in the page. Attribute value is to label for the button and action is to refer a method in standard or custom Controller.

<apex:inputField> – to refer value of a field from salesforce object. When this is wrapped within pageblockSection, the it displays the label from the object field.

<apex:form> – must be included if we use any input or output components in the page.

Custom Controller – is your apex class to write business logic. We will see much more detail about this soon.

Leave a Comment