Sunday 21 October 2012

Displaying Visualforce Components from Apex Class using DynamicComponent


Here i am trying to explain to display the visualforce components from apex class by using DynamicComponent Tag.

On every component we have

Dynamic Visualforce Notation in component reference for example


for <apex:inputField> we have Dynamic Visualforce Notation is Component.Apex.InputField
By using this type of notations i build search page for searching Account records by account name.
By using this we can control the visualforce components from Apex Class.

Controller:
=============
public with sharing class DynamicComponentExample11 {
    public DynamicComponentExample11(ApexPages.StandardController con) { }
    public string enterstring{get;set;}
    public List<Account> lstaccount{get;set;}
    public Component.Apex.SectionHeader getHeaderWithDueDateCheck() {
        date dueDate = date.newInstance(2011, 7, 4);
        boolean overdue = date.today().daysBetween(dueDate) < 0;

        Component.Apex.SectionHeader sectionHeader = new Component.Apex.SectionHeader();
        if (overdue) {
            sectionHeader.title = 'This Form Was Due On ' + dueDate.format() + '!';
            return sectionHeader;
        } else {
            sectionHeader.title = 'Form Submission';
            return sectionHeader;
        }
    }
 
    Public List<Account> getAccounts(){
     
        if(enterstring!=null && enterstring!='')
            lstaccount=[select id,name,type,industry from account where name like:enterstring+'%'];
        else
            lstaccount=[select id,name,type,industry from account];
        return lstaccount;
 
    }
    public void search1(){
        getAccounts();
     
    }


 public Component.Apex.PageBlock getpageBlock(){
        Component.Apex.PageBlock pb=new Component.Apex.PageBlock();
        Component.Apex.PageBlockTable  ptable=new Component.Apex.PageBlockTable();
        Component.Apex.OutputLabel optlabel=new Component.Apex.OutputLabel();
        Component.Apex.CommandButton cmdbtn=new Component.Apex.CommandButton();
        cmdbtn.value='Search';
        cmdbtn.expressions.action='{!search1}';
        optlabel.value='Enter Name';
        Component.Apex.Inputtext intext=new Component.Apex.InputText();
        intext.id='searchtext';
        intext.expressions.value='{!enterstring}';
        optlabel.for='intext';
        ptable.expressions.value='{!accounts}';
        ptable.var='a';
        if(enterstring!=null && enterstring!='')
         ptable.rendered=true;
        else
         ptable.rendered=false;
        Component.Apex.Column namecolumn=new Component.Apex.Column();
        namecolumn.expressions.value='{!a.name}';  
        Component.Apex.Column industrycolumn=new Component.Apex.Column();
        industrycolumn.expressions.value='{!a.Industry}';
        Component.Apex.Column typecolumn=new Component.Apex.Column();
        typecolumn.expressions.value='{!a.type}';
        pb.childcomponents.add(optlabel);
        pb.childcomponents.add(intext);
        pb.childcomponents.add(cmdbtn);
        pb.childcomponents.add(ptable);
        ptable.childcomponents.add(namecolumn);
        ptable.childcomponents.add(typecolumn);
        ptable.childcomponents.add(industrycolumn);
        return pb;
    }
}
=====================
Visualforce page
============================

<apex:page standardcontroller="Account" extensions="DynamicComponentExample11">
    <apex:form >
           <apex:dynamicComponent componentValue="{!pageBlock}"/>
    </apex:form>
</apex:page>

=======================
Output:
======




3 comments:

  1. Hi there

    I'm reading and trying those same examples as you have here. I'm assuming the due date example you have is from the Visualforce Developer's Guide book.

    I would like to ask you if you ever try the example that follows, where the code is

    Component.Apex.Detail detail = new Component.Apex.Detail();
    detail.expressions.subject = '{!Account.ownerId}';
    detail.relatedList = false;
    detail.title = false;

    If I have my VF page with Account as the standard controller and an extension that includes the code above, then I get this error message "SObject row was retrieved via SOQL without querying the requested field". The only workaround I have is to add Account.ownerId into the VF page (set rendered=false). But to me, that defeats the "dynamic" part of the dynamic visualforce component concept. I would like to know if I'm not doing something right. Thanks.

    ReplyDelete
  2. Thanks Balaji. Its help full to me alot.
    keep on posting like these new concepts in salesforce

    ReplyDelete
  3. Hi balaji,

    im trying to use a command link in the column of a pageblock in dynamic component and use a param value for the controller, can u provide an example for that.

    thanks,
    Sandeep

    ReplyDelete

How to include a screen flow in a Lightning Web Component

 Hi, Assume  you have a flow called "Quick Contact Creation" and API Name for the same is "Quick_Contact_Creation". To i...