The <apex: dynamicComponent> is a component tag used for our dynamic apex components. It acts as a placeholder and has one required parameter, i.e., componentValue. The componentValue is the name of the apex method that returns the dynamic component.
These are some of the following components which don't have dynamic Apex representations:
<apex: attribute>
<apex: component>
<apex: componentBody>
<apex: composition>
<apex: define>
<apex: dynamicComponent>
<apex: include>
<apex: insert>
<apex: param>
<apex: variable>
The <apex: dynamicComponent> has the following attributes:
1. componentValue
The componentValue is a UIComponent type attribute used for specifying the name of the apex method that returns the dynamic VF component.
Syntax:
<apex:dynamicComponent componentValue="{!dynamicDetail}" />
2. id
The id is a string type attribute, i.e., a unique identifier that allows this component to be referenced by other components in the custom component definition.
Syntax:
<apex:dynamicComponent componentValue="{!dynamicDetail}" id="dynamicId" />
3. invokeAfterAction
The id is a Boolean type attribute that specifies whether the componentValue's apex method is called after the page's or submit's action method is invoked.
Syntax:
<apex:dynamicComponent componentValue="{!dynamicDetail}" invokeAfterAction="true" />
4. rendered
The rendered is a Boolean type attribute that specifies whether this component needs to be rendered on the page or not. The boolean value true is set as a default value to this attribute.
Syntax:
<apex:dynamicComponent componentValue="{!dynamicDetail}" rendered="false" />
Let's take an example to understand how we can use this component on the VF page:
ApexDynamicComponentExample.vfp
AD
<apex:page controller="DynamicComponentController">
<apex: dynamicComponent componentValue="{!dynamicDetail}" />
</apex: page>
DynamicComponentController.apxc
// create DynamicComponentController class for specifying the apex method that returns the dynamic component
public class DynamicComponentController {
// create getDynamicDetail() method that returns dynamic component
public component.Apex.Detail getDynamicDetail() {
// create an instance of the Detail component
Component.Apex.Detail detail = new component.Apex.Detail();
//set subject and other details
detail.expressions.subject = '{!acct.OwnerId}';
detail.relatedList = false;
detail.title = false;
return detail;
}
// getter for returning the 1st account
public Account acct {
get { return [SELECT Id, Name, OwnerId FROM Account LIMIT 1]; }
}
}