2

I have this code

<apex:outputText value="{!item['text_1']}" />
<apex:outputText value="{!item['text_2']}" />

But item['text_1']} could be empty, if this is empty show text_2

I tried this code but I get a Syntax Error

<apex:outputText value='{!IF({!item["text_1"]} != "", {!item["text_1"]},{!item["text_2"]})}' />
  • FYI your questions will be much more useful to the community (and might even receive more votes) if you include the actual text of your error messages verbatim. – Adrian Larson Mar 10 '17 at 19:40
  • Thats the actual error message ` Syntax Error` I use mavensmate with sublime text thats the error who shows me – xtornasol512 Mar 10 '17 at 20:22
9

In this specific case, it would be marginally more efficient to use BLANKVALUE, just as you would with a formula:

<apex:outputText value="{!BLANKVALUE(item['text_1'],item['text_2'])}" />

A merge expression begins with {! and ends with }. You do not need to use the syntax recursively. Once you're in "expression mode", you stay there until you exit it with the closing curly brace.

  • Ah, nice use of BlankValue method, almost never had got chance to use it. Thanks +1 – Raul Mar 10 '17 at 19:42
5

You can use rendered property to display them conditionally:

<apex:outputText value="{!item['text_1']}" rendered="{NOT(ISBLANK(item['text_1']))}"/>
<apex:outputText value="{!item['text_2']}" rendered="{NOT(ISBLANK(item['text_2']))}"/>

**Added:

Use IF condition along with blank check in value property, here is a correct syntax:

<apex:outputText value="{!IF(NOT(ISBLANK(item['text_1'])), 
    item['text_1'], 
    item['text_2'])}"/>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.