Menu Close
    Hire us Request quote Reading list Switch to dark mode
    lockdownWebkulUp

    Using If condition in visualforce

    If statement is the most basic requirement of any developer. This statement has exist in every High Level Language, as writing a code without using this condition seems next to impossible. However we do know that for languages like HTML if condition is not required, as HTML is nothing more than a markup language, yet the support is provided with the help of JavaScript to vary elements on the basis of conditions. Salesforce on the other hand, took a step forward and provided this conditional statement in Visualforce itself. We will discuss about the same today, using If condition in salesforce.

    APEX Code

    For this example I have written an APEX class to assign 5 accounts originally to a list. There is also a function which assigns all the records to that list.

    VisualForce Code

    Before jumping onto the code itself, let’s discuss something. How to actually use the If condition? Using it is really simple and can be somewhat related to the usage of ternary operator. First of all, it must be in the formula expression. It must start with If, followed by parenthesis. The parenthesis will take 3 arguments, first the condition, in parenthesis again, second the value if true is returned, and the last argument, like you guessed, will be the value of condition is false.
    The values can be any apex controller variable, or it can be strings written directly.


    As you can see that the disabled attribute of the commandbutton is using the If condition, also the condition is nested. Now while nesting the if condition in Visualforce you need to be really careful of the parenthesis just like you need to be careful of scope normally.

    Output

    The Output of my code will be something like this:

    . . .
    Subscribe Articles

    Just enter your email below to subscribe and receive weekly updates about the most interesting similar articles.

    You need to accept conditions to receive letters.
    You've Subscribed!

    Share article via

    . . .

    Support

    That’s all about using If condition in visualforce, for any further queries feel free to add a ticket at:

    https://webkul.uvdesk.com/en/customer/create-ticket/

    Or let us know your views on how to make this code better, in comments section below.

    . . .

    Comment

    Add Your Comment

    Be the first to comment.

    Back to Top
    css.php
    Hire Us!
    Brief us about your requirements and we'll get back to you.
    Woo! Hooy!
    We have just recieved your project brief and our expert will contact you shortly.
    Send Again
    Close

    Table of Content

    Hide Index