Thursday, 11 February 2016

Salesforce Dynamic Visualforce Components

Use Salesforce Dynamic Visualforce Components and Custom Label for predefined String(helpful for displaying Apex Messages etc)

Visualforce is mainly intended for create static pages by using Different namespace tag and dynamically populating the values based on controller binded variables but sometimes Developers need to customize the Pages in such a way to reduce maintainance Development effort and the changes could be carried out in few point and clicks. To adapt this strategy, Salesforce provided -"Dynamic Visualforce Components", Under this approach VF components are dynamically populated in APEX class and then rendered onto the VF page.

Custom Label usage: Developers can use Custom Labels in Apex by using following expressions - System.Label.labelName

VF Page :
1:  <apex:page standardController="Contact" extensions="DynCompExample">  
2:    <apex:dynamicComponent componentValue="{!headerWithDueDateCheck}"/>  
3:    <apex:dynamicComponent componentValue="{!ErrorLabelString}"/>  
4:  </apex:page>  

APEX Class:
1:  public class DynCompExample {  
2:    public DynCompExample(ApexPages.StandardController con) { }  
3:    public Component.Apex.SectionHeader getHeaderWithDueDateCheck() {  
4:      date dueDate = date.newInstance(2016, 2, 8);  
5:      boolean overdue = date.today().daysBetween(dueDate) < 0;  
6:      Component.Apex.SectionHeader sectionHeader = new Component.Apex.SectionHeader();  
7:      if (overdue) {  
8:        sectionHeader.title = 'This Form Was Due On ' + dueDate.format() + '!';  
9:        return sectionHeader;  
10:      } else {  
11:        sectionHeader.title = 'Form Submission';  
12:        return sectionHeader;  
13:      }  
14:    }  
15:       // This method return the String value for the Error Label id   
16:    public Component.Apex.OutputText getErrorLabelString(String labelName){   
17:            Component.Apex.OutputText outputError = new Component.Apex.OutputText();   
18:            outputError.expressions.value = '{!$Label.' + labelName + '}';   
19:            return outputError;   
20:       }   
21:  }  

Note from Salesforce : Dynamic Visualforce components are not intended to be the primary way to create new Visualforce pages in your organization. Existing Visualforce pages shouldn’t be rewritten in a dynamic manner and, for most use cases, standard Visualforce components are acceptable and preferred. You should only use dynamic Visualforce components when the page must adapt itself to user state or actions in ways that can’t be elegantly coded into static markup.

Documentation  Link : https://help.salesforce.com/HTViewHelpDoc?id=cl_about.htm&language=en_US

No comments:

Post a Comment