Wednesday 5 September 2012

Basic Formulas - Visualforce


Visualforce lets you embed more than just variables in the expression language. It also supports formulas that let you manipulate values. The & character is the formula language operator that concatenates strings.
  1. Add this to your Visualforce page: {! $User.firstname & ' ' & $User.lastname}
    This tells Visualforce to retrieve the firstname and lastname fields from the global User object, and to concatenate them with a space character. The output will be something like: Joe Bloggs.
    In general, formulas are slightly more advanced and have a simple syntax that includes the function name, a set of parentheses, and an optional set of parameters.
  2. Add this to your Visualforce page:
    <p> Today's Date is {! TODAY()} </p>
    <p> Next week it will be {! TODAY() + 7} </p>
    You’ll see something like this in the output:
    Today's Date is Wed Feb 08 00:00:00 GMT 2012
    Next week it will be Wed Feb 15 00:00:00 GMT 2012
    The <p> tags are standard HTML for creating paragraphs. In other words, we wanted both sentences to be in individual paragraphs, not all on one line. The TODAY()function returns the current date as a date data type. Note how the time values are all set to 0. Also note the + operator on the date. The expression language assumes you want to add days, so it added 7 days to the date.
  3. You can use functions as parameters in other functions, and also have functions that take multiple parameters too. Add this:
    <p>The year today is {! YEAR(TODAY())}</p>
    <p>Tomorrow will be day number  {! DAY(TODAY() + 1)}</p>
    <p>Let's find a maximum: {! MAX(1,2,3,4,5,6,5,4,3,2,1)} </p>
    <p>The square root of 49 is {! SQRT(49)}</p>
    <p>Is it true?  {! CONTAINS('salesforce.com', 'force.com')}</p>
    The output will look something like this:
    The year today is 2012
    Tomorrow will be day number 9
    Let's find a maximum: 6
    The square root of 49 is 7.0
    Is it true? true
    The CONTAINS() function returns a boolean value: something that is either true or false. It compares two arguments of text and returns true if the first argument contains the second argument. If not, it returns false. In this case, the string “force.com” is contained within the string “salesforce.com”, so it returns true.

No comments:

Post a Comment