Salesforce Developers Blog

Building Dynamic Visualforce Page Layouts

Avatar for Samantha ReadySamantha Ready
Have you ever wanted a page or form to dynamically refactor itself based off of user selections? If you want to conditionally hide or show fields based off of selections on the page, you can do so using only a few standard Visualforce components and a standard controller!
June 04, 2013
Listen to this article
0:00 / 0:00

Have you ever wanted a page or form to dynamically refactor itself based off of user selections? Record types on page layouts are useful, but there are instances where you might want to conditionally hide or show fields based off of selections on the page. Thankfully this is possible using a few standard Visualforce components and a standard controller!

Displaying Visualforce Fields or Sections Dependent on Data

In order to create this functionality, you will need to add three main elements to your form:

    1. <apex:actionRegion>: You will need to add an action region within your form around the controlling field components. This will distinguish what region of the form should be processed by Force.com when an AJAX request is generated.
    2. <apex:actionSupport>: This component controls the behavior of the form and adds AJAX support to your actionRegion. If you add this component directly below a field, you can associate DOM events to that field to generate an AJAX request. This component only provides support for invoking custom or standard controller action methods from other Visualforce components, but if you would like to invoke controller methods in JavaScript you can use actionFunction.
    3. <apex:__?__ … rendered=”{!MyObject.Field == ‘field value’}” … >: Add conditional logic within the rendered attribute to the component(s) you want to conditionally show.
The sample below shows a simple ‘New Case’ edit page, where a section with a few custom fields will render if the case type ‘Electronic’ is selected in the form. Because we are using the AJAX components above, this will happen dynamically on change of the case type picklist value.
1<apex:page standardController="Case" sidebar="false">
2    <apex:sectionHeader title="Edit Case" subtitle="{!Case.CaseNumber}"/>
3    <apex:form >
4        <apex:pageBlock title="Edit Case" id="thePageBlock" mode="edit">
5            <apex:pageMessages />
6            <apex:pageBlockButtons >
7                <apex:commandButton value="Save" action="{!save}"/>
8                <apex:commandButton value="Cancel" action="{!cancel}"/>
9            </apex:pageBlockButtons>
10            <apex:actionRegion >
11                <apex:pageBlockSection title="Case Information" columns="1">
12                    <apex:inputField value="{!Case.Subject}"/>
13                    <apex:pageBlockSectionItem >
14                        <apex:outputLabel value="Type"/>
15                        <apex:outputPanel >
16                            <apex:inputField value="{!Case.Type}">
17                                <apex:actionSupport event="onchange" rerender="thePageBlock"
18                                                    status="status"/>
19                            </apex:inputField>
20                            <apex:actionStatus startText="applying value..." id="status"/>
21                        </apex:outputPanel>
22                    </apex:pageBlockSectionItem>
23                    <apex:inputField value="{!Case.Status}"/>
24                    <apex:inputField value="{!Case.Origin}"/>
25                    <apex:inputField value="{!Case.Description}"/>
26                </apex:pageBlockSection>
27            </apex:actionRegion>
28            <apex:pageBlockSection title="Electronic Problem Detail" columns="1"
29                                   rendered="{!Case.Type == 'Electronic'}">
30                <apex:inputField value="{!Case.Bug_Number__c}"  required="true"/>
31                <apex:inputField value="{!Case.Replacement_Needed__c}"/>
32            </apex:pageBlockSection>
33        </apex:pageBlock>
34    </apex:form>
35</apex:page>

Create Your Own Conditional Custom Visualforce Page

Now that you’ve seen this example, you can see how simple it can be to add conditional logic to make dynamic custom layouts. If you are unfamiliar with building custom pages on Force.com, you can find a similar example here with more context into how each element the MVC model comes into play.

More Blog Posts

Using AI Model Output in Data Cloud with Prediction Jobs

Using AI Model Output in Data Cloud with Prediction Jobs

Use Einstein Studio prediction jobs to use AI models to score records sourced from Data Cloud. Automate business rules based on the results of your predictions.December 10, 2024

Introducing Local File Upload Connector in Data Cloud (Beta)

Introducing Local File Upload Connector in Data Cloud (Beta)

The new Local File Upload connector makes it easy to quickly get small amounts of data into Data Cloud for testing or validation.February 20, 2025