Using Merge fields in Visualforce pages

Merge fields are an important part of building visualforce pages. They allow you to embed property or object values in your apex controller directly into the HTML generated by the corresponding Visualforce page. Let’s look at an example to understand that statement:
Visualforce Page
    
 
Apex Controller
public class MergeFields_Controller 
{    
    public DateTime CurrentTime
    {
        get{
            return DateTime.Now();
        }
    }
}


In the above sample, we have created a simple Visualforce page and its associated controller. CurrentTime is a read-only property defined in the controller that always returns the current date & time. The property is displayed on the page using the merge field expression {!CurrentTime}.
Type Conversions with merge fields

When displaying merge fields as text, Salesforce automatically converts the property value to String data type. This has the same effect as called the toString() method on the property. Example below:
Visualforce Page

    
    
    


Apex Controller
public class MergeFields_Controller 
{
    public class C1
    {
        public integer i;
        C1(integer val)
        {
            i=val;
        }
    }        
    public DateTime CurrentTime
    {
        get{
            return DateTime.Now();
        }
    }    
    public C1 theObject
    {
        get{
            return new C1(50);
        }
    }    
    public String theObject_String
    {
        get{
         return theObject.toString();
        }
    }
}


In the above code, I am rendering three different properties using merge fields. CurrentTime is of type DateTime, whereas theObject is of type C1, which is a new class that I have defined. Lastly, theObject_String is a String representation of theObject by calling the toString() method on the object. In the output below, you can see that the object is rendered as a string when used in a merge field.



image

Using Apex in merge fields

A common misunderstanding is trying to use Apex code in merge fields – merge fields do not allow Apex code. Instead, merge fields are similar to formula fields, and you can use most expressions used in formula fields with merge fields. For e.g., use the following expression to display the current date/time:
  
The above merge expression will display something like
8/10/2014 9:30 PM.

Not all functions that can be used in formula fields can be used in merge expressions. For e.g., the following will throw an error when used in a merge field, even though you can use it in a formula field:
 

A good rule of thumb is that formula expressions which do not add new non-text elements (like images, hyperlinks etc.) to the visualforce page are prohibited with merge fields.
Type Conversion **conditions apply
Not all merge field expressions are converted automatically to String. Automatic type conversion happens only when the property is used directly in the merge field, and is not used with any formulas. If used with formulas, the property value is not converted to string during formula evaluation. Rather, the formula expression is first evaluated, and the result is then converted to String. As an example, in the below code sample, the expression {!LEN(CurrentTime)} will not work because the LEN() function expects an argument of type String, whereas CurrentTime is of type DateTime. :
Visualforce Page


     

    

Apex Controller
public class MergeFields_Controller 
{       
    public DateTime CurrentTime
    {
        get{
            return DateTime.Now();
        }
    }
}
Input data using merge fields
Merge fields are also used to accept input data from users. For e.g., the below code accepts text input from the user using <apex:inputText>, and then displays the input text back on the screen using an <apex:outputText>.
Visualforce Page


    

    Enter the value of str :                  
    Enter the length of str :    

Apex Controller
public class MergeFields_Controller 
{
    public String str
    {
        get; set;
    }
}

Note that in the above example, we're accepting an input value in the , and assigning it to the property str. Also note that you cannot use formulas when using merge fields to accept user input.

No comments:

Post a Comment