1.5 Use standard actions from standard controller

The Concept

In the last lecture we have demonstrated how to use standard controller to retrieve information from SObject records. But only display them is not that interesting. In this course, I will show you how to make them editable.

The Code

Create a new Visualforce page called: MerchandiseEdit with the following code:

<apex:page standardController="Merchandise__c" >
    <apex:sectionHeader title="Merchandise" subtitle="Choose List"/>
    <apex:form >
    <apex:pageBlock title="Merchandise">
        <apex:pageBlockButtons >
            <apex:commandButton action="{!Save}" value="Save"/>
            <apex:commandButton action="{!cancel}" value="Cancel" immediate="true"/>
        </apex:pageBlockButtons>
        <apex:pageBlockSection title="Merchandise Info" columns="1">
            <apex:inputField value="{!Merchandise__c.Original_Price__c}"/>
            <apex:inputField value="{!Merchandise__c.Discount_Type__c}"/>
            <apex:outputField value="{!Merchandise__c.Discounted_Price__c}"/>
        </apex:pageBlockSection>
    </apex:pageBlock>
    </apex:form>
</apex:page>

Try the page out the same way as in our previous lecture: https://[Your Salesforce instance]/apex/MerchandiseEdit?Id=[Your record Id]

And pay attention to those highlighted lines.

Some Brief Explanations

apex:sectionHeader looks fancy. It is very easy to understand and handy to use. It adds the Salesforce style header to the page.

Please pay attention to the apex:form tag. Everything inside that is in a form. So basically if you want to submit something to the back end or if you want the information to be able to send back to the server side, make sure it is inside the apex:form tag.

We used the buttons with standard actions to talk to our server side. So once we changed the original price and clicked save, it will be updated in the Salesforce database.

In the cancel action, there is an attribute called immediate=”true”. That means by executing this standard action, we don’t need to check whether the form is in the right state or not. So if the required field is not filled out, it will not block the execution of cancel.

The values inside apex:inputFields are two-way binding. So if you change a value on the page, it also changes the related value inside the controller. We will talk more about this in the later chapters.

Next Post

2.1 When standardcontroller is not sufficient

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 *