2

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>

2 Answers 2

4

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)}"
3

I figured it out, I knew it was something simple:

<apex:form>
  <apex:commandbutton id="MarkReviewBtn" rendered="{!!testResultObj.ReviewedByHCP}" styleClass="btn btnGreen" value="{!$Label.JC_CM_Mark_as_Reviewed}" 
 onclick="markAsReviewed()" action="{!markAsReviewed}"/>
</apex:form>
2
  • 2
    It is way less confusing if you use NOT(condition) instead of !condition in a merge field so you don't get two ! characters in a row.
    – Adrian Larson
    Commented Mar 14, 2016 at 17:07
  • 1
    @AdrianLarson ...or some whitespace {! !testResultObj.ReviewedByHCP }
    – Keith C
    Commented Mar 14, 2016 at 18:25

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .