1.4 Use standard controller to connect to an SObject

The Concept

Till now, what we have introduced about Visualforce is still just a container of ordinary html page. But what really makes Visualforce shining out is its seamless interaction with Salesforce platform.

Let’s first start with the powerful standardcontroller.

The Code

Let’s first assume we already has the Merchandise SObject created. If you haven’t done so, follow the instructions at: What is SObject and how do we use it in Apex 

Now create a Visualforce page called MerchandiseDisplay with the following code:

<apex:page standardController="Merchandise__c ">
    <style>
        h1 {
            font-size: 20px
        }
    </style>
    <h1>
        {!Merchandise__c.Name}
    </h1>
    <apex:pageBlock>
        <apex:pageBlockSection title="Merchandise display">
            <apex:outputField value="{!Merchandise__c.Name}"/>
            <apex:outputField value="{!Merchandise__c.Original_Price__c}"/>
            <apex:outputField value="{!Merchandise__c.Discount_Type__c}"/>
            <apex:outputField value="{!Merchandise__c.Discounted_Price__c}"/>
        </apex:pageBlockSection>
    </apex:pageBlock>
</apex:page>

Find an existing merchandise record. If you don’t have one yet, just create one. Get the Id of the merchandise record. Then in the browser, type in this url:

https://[Your Salesforce instance]/apex/MerchandiseDisplay?Id=[Your record Id]

Replace [Your Salesforce instance] and [Your record Id] with your ones.

If you don’t know how to find the Id of a Salesforce record, it is just in your browser’s url when you are viewing the record. For example, my url for the record is: https://ap7.salesforce.com/a000I00001PDsKj. So my merchandise record’s id is: a000I00001PDsKj.

Your page should look similar to this:

Some Brief Explanations

I know those tags like apex:pageBlock and apex:pageBlockSection might be new to you. But let’s not focus on them right now. They are not that hard when you get used to them. Visualforce tags start with Apex:

Let’s focus on how we retrieved the SObject’s information into the page. We did it by first linking the page to a standard controller in the first line: standardController=”Merchandise__c “

Then later on, when we want to retrieve some field information, we basically use this format: {![SObjectName].[FieldName]}

Please note, both SObjectName and FieldName should be API names. And it can be used both as an attribute in apex:xxx tags or separately – like the one inside h1 tag.

Later on, we will learn how to use Apex variable to retrieve those values. For now, we just use the SObjectName to do the job.

Read the code again and try to understand how it works. Especially the getting field value ones.

Next Post

1.5 Use standard actions from standard controller

Subscribe to Sfdcinpractice

Subscribe to get the latest blogs and tutorials of sfdcinpractice. No spam, no trash, only the awesome posts from sfdcinpractice. 

Leave a Reply

Your email address will not be published / Required fields are marked *