Wednesday, 5 September 2012

Conditionals - Visualforce


Sometimes you want to display something dynamically, based on the value of an expression. For example, if an invoice has no line items, you might want to display the word “none” instead of an empty list, or if some item has expired, you might want to display “late” instead of showing the due date.
You can do this in Visualforce by using a conditional formula expression, such as IF(). The IF() expression takes three arguments:
  • The first is a boolean: something that is either true or false. You’ve seen an example of that in the CONTAINS() function.
  • The second argument is something that will be returned if the boolean is true.
  • The third argument is something that will be returned if the boolean is false.
Insert the following and try to predict what will be displayed if you save the page:
{! IF ( CONTAINS('salesforce.com','force.com'), 'Yep', 'Nah') }
{! IF ( DAY(TODAY()) > 14, 'After the 14th', 'On or before the 14th') }
You’ll see something like this:
Yep
On or before the 14th
Of course, this all depends on when you run the code. After the 14th in a month, it looks different.

No comments:

Post a Comment