Am sure by now you would have know that we can use ternary operators in Visualforce pages.

In case you are not aware, this is how the syntax is going to look like.

{! IF(condition, what_if_true, what_if_false) }

Couple of use cases where we would use this is

  • When we want to conditionally show/hide UI elements or data on the Visualforce page.
  • Conditionally assign a CSS class to the tags and etc.

This made me think what are the other functions that can we use in Visualforce similar to IF( ) function.

This is how my thought process was, this syntax was kind of similar to the IF( ) function we use when we try to create formula fields, validation rules etc.

Something like this!

functions panel when we try to create formula fields on objects

Then I tried using one of the functions in the formulae editor in the Visualforce page.

{! TODAY() + 30 }

and that is it, it's working.

That’s when I realised we can use all the functions that we get access to when we try to create a formula field in our Visualforce page.

Amazing isn't it!

Some other use cases where these functions will be helpful is

  1. TODAY() Returning the current date, so that you don't have to create a variable in the Apex class, use a variable and use setters and getters assigned to it.
  2. CASESAFEID(id) Converting the 15 digit ID to 18 digit ID
  3. MCEILING(number) Rounds a number up to the nearest integer, towards zero if negative