[SalesForce] “rendered” usage with variable

I am trying to use "rendered" based on the two conditions below.

I have created variable called "output" to pass the value of "true" and "false" to Visualforce page.

The issue that I am having is that the first condition works, but the second condition does not work.

First condition would not display any output. It works (it does not display data).

The second condition would display output as normal. It does not work.

I am curious whether location of " output = " inside IF Statement is causing the issue.

Bottom is APEX code:
I modified the code after.

        public Boolean output {get;set;} 


        // First Condition: When MRN is blank 
        if( !string.isBlank(tr.mrn))            
        {
            if (RowList.size() > 1){
                ApexPages.addMessage(
                New ApexPages.Message(
                ApexPages.Severity.FATAL, 'Multiple records found. Please try with MRN'
                ));
                return null;                       

              }
              output = false;
        }

        else
        {
          // Second Condition: When MRN is Not blank
        output = true; 
        }    

Bottom is VF code:

<apex:pageBlockTable value="{!RowList}" var="c" rendered="{!output}">
            <apex:column >
                <apex:facet name="header">MRN</apex:facet>
                {!c.mrn}
            </apex:column>
            <apex:column >
                <apex:facet name="header">Last Name</apex:facet>
                {!c.lname}
            </apex:column>
            <apex:column >
                <apex:facet name="header">First Name</apex:facet>
                {!c.fname}
            </apex:column>                         
         </apex:pageBlockTable>         

Best Answer

rendered="{!output}" evaluates boolean expression.

In your code output is String type. Change it to Boolean like this.

public Boolean output {get;set;} 

// First Condition: When MRN is blank 
if( !string.isBlank(tr.mrn))            
{
    if (RowList.size() > 1){
        ApexPages.addMessage(
        New ApexPages.Message(
        ApexPages.Severity.FATAL, 'Multiple records found. Please try with MRN'
        ));
        return null;                       

      }
      output = false;
}
else
{
      // Second Condition: When MRN is Not blank
    output = true; 
} 

Otherwise, use rendered="{!output=='true'}"

Update

According to your logic, data will be displayed in following conditions:

When output is true, string.isBlank(tr.mrn) is blank and RowList.size() > 1

So, I think you have wrongly written the logic. According to the comment in the code, the correct logic will be as follows:

// First Condition: When MRN is blank 

if(string.isBlank(tr.mrn))            
{
    if (RowList.size() > 1){
        ApexPages.addMessage(
        New ApexPages.Message(
        ApexPages.Severity.FATAL, 'Multiple records found. Please try with MRN'
        ));
        return null;                       

      }
      output = false;
}
else
{
    // Second Condition: When MRN is Not blank
    output = true; 
} 
Related Topic