Dynamic Visualforce Components Example:

Example of generate a dynamic input form for Account object

VisualForce Page:

<apex:page standardController="Account" extensions="DynamicAccountForm">
<!-- Create section header dynamically -->
<apex:dynamicComponent componentValue="{!SectionHeader}"/>
<apex:form >
<!-- Create dynamic input form for account object -->
<apex:dynamicComponent componentValue="{!ActionForm}"/>

Apex Class:

public with sharing class DynamicAccountForm
    public DynamicAccountForm(Apexpages.standardController ctlr)
    //Create a page block dynamically
    public Component.Apex.PageBlock getActionForm()
        Component.Apex.PageBlock pb = new Component.Apex.PageBlock();

        //creating an input field dynamically
        Component.Apex.InputField name = new Component.Apex.InputField();
        name.expressions.value = '{!Account.Name}';
        name.id = 'name';
        Component.Apex.OutputLabel label = new Component.Apex.OutputLabel();
        label.value = 'First Name';
        label.for = 'name';
        //Use the above block to create other input fields      
        Component.Apex.CommandButton save = new Component.Apex.CommandButton();
        save.value = 'Save';
        save.expressions.action = '{!Save}';
        return pb;

    //create section header dynamically
    public Component.Apex.SectionHeader getSectionHeader()
        Component.Apex.SectionHeader sh = new Component.Apex.SectionHeader();
        sh.title = 'Create Account';
        return sh;

Written by 

I am Sakthivel Madesh, a certified Salesforce Advanced Developer & Administrator working on Salesforce Technology since 2011. I am currently working in Techforce Services as Sr. Salesforce Developer. I have worked on multiple technologies Like PHP, Java, Salesforce, I am very passionate about Salesforce and to earn 15x Salesforce Certification. I love Trailhead for learning Salesforce Skill and Become a Trailhead 5x Ranger too. I Love to Learn & Share my Salesforce knowledge to Salesforce Learners/Beginner using My Blog and Salesforce Community.

Leave a Reply

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