Visualforce – Variables & Formulas
The data from the Salesforce sObjects(Standard/Custom) can be brought into the Visualforce page using variables and expressions. Like other language, the Visualforce language has its own expressions, operators and literals, etc. The basic building block of this language is an expression.
The syntax of a Visualforce expression is as given below:
{! expression }
Example
In this scenario, let us use the global variable named as $user (it is a global variable which is a type of merge field that yields information about the org or the current user of the org. Each global variable is easily identified because of it unique starts with a dollar sign “$”.). We can write the following piece code to get logged in username, First name and login name.
<apex:page> <apex:form > <h1> Welcome to Visualforce page development. </h1> <apex:pageblock title="Page block title here"> <apex:pageblockSection columns="3" title="Page block section title here"> {!$User.FirstName} {!$User.LastName} </apex:pageblockSection> </apex:pageblock> </apex:form> </apex:page>
On clicking on preview the page, we get the following output.
Likewise, we can use many other variables, we can do manipulate them using formula as shown in below code:
<apex:page> <apex:form > <h1> Welcome to Visualforce page development. </h1> <apex:pageblock title="Page block title here"> <apex:pageblockSection columns="3" title="Page block section title here"> 2 multiply with 10 = {! 2 * 10 } </apex:pageblockSection> </apex:pageblock> </apex:form> </apex:page>
On clicking on preview the page, we get the following output.