[SalesForce] Render command button based on boolean if condition

I want my command button to be visible if a boolean variable "ReviewedByHCP" is set to false, and hidden if it's set to true.

Here's what I have:

<apex:form>
    <apex:commandbutton id="MarkReviewBtn" rendered="{!IF(testResultObj.ReviewedByHCP == false), true,false}" styleClass="btn btnGreen" value="{!$Label.JC_CM_Mark_as_Reviewed}" 
           onclick="markAsReviewed()" action="{!markAsReviewed}"/>
</apex:form>

Best Answer

The reason your logic failed was that your close paren is in the wrong spot in your merge syntax. Correct IF syntax is IF(logical_test, value_if_true, value_if_false).

Incorrect:

rendered="{!IF(testResultObj.ReviewedByHCP == false), true,false}"

Correct:

rendered="{!IF(testResultObj.ReviewedByHCP, false, true)}"

However, even the correct syntax is excessively verbose. It demonstrates a common anti-pattern in merge syntax, namely:

Any condition written IF(condition, true, false) can be rewritten as condition.

The corollary here is that:

Any condition written IF(condition, false, true) can be rewritten as NOT(condition)

So in summary, as you noted, negation is much simpler.

rendered="{!NOT(testResultObj.ReviewedByHCP)}"
Related Topic