function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
dwwrightdwwright 

Boolean logic in VF pages

I want to dynamically render sections of a page based on picklist values. To do that, I'm utilizing the "rendered=" attribute, and evaluating the values of a picklist to determine whether the section should display or not. My code is below

<apex:variable var="SEL" value="My_Object__c.Selection__c"/> <apex:pageBlock id="Section 123" rendered="{!SEL == '123'}"> <h1>This section represents Section 123</h1> </apex:pageBlock>

 In this case, I want Section 123 to display whenever the picklist value is "123". Can someone help with this?

Best Answer chosen by Admin (Salesforce Developers) 
bob_buzzardbob_buzzard

Interesting.  I was using a standard object, and it worked okay. I've just coded up an example using the objects from the sample recruiting app, and I got the error that you saw, so it seems there is a bit of a disconnect. 

 

However, the following behaved as expected:

 

 

<apex:page standardController="Candidate__c"> <apex:outputField value="{!Candidate__c.Education__c}"/> <apex:outputPanel rendered="{!Candidate__c.Education__c=='High school'}"> <br/> <apex:outputText value="Should see this"/> </apex:outputPanel> <apex:outputPanel rendered="{!Candidate__c.Education__c!='High school'}"> <apex:outputText value="Shouldn't see this"/> </apex:outputPanel> </apex:page>

 

Is this any different to your experience?

 

 

 

All Answers

bob_buzzardbob_buzzard

Try the ISPICKVAL function, without assigning your value to a variable - that's worked for me in the past.  E.g.:  

 

{!ISPICKVAL(My_Object__c.Selection__c, '123')}

dwwrightdwwright

I tried that, but I've been unsucessful getting the VF page to accept that syntax. The syntax you posted, which in my case looks like this:

 

rendered="{!ISPICKVAL(Quality_Document__c.Breaker__c,'MSV')}"

 

returns a message that says "Error: Incorrect parameter for function ISPICKVAL(). Expected Picklist, received Text"... and I'm definitely using a SINGLE picklist field. In desperation I also tried it like this:

 

<apex:pageBlock id="SDQA178" rendered="{!ISPICKVAL({!Quality_Document__c.Breaker__c},'MSV')}"

 

But receive just a plain "syntax error." But apparently you've made it work Bob, any pointers?

bob_buzzardbob_buzzard

Interesting.  I was using a standard object, and it worked okay. I've just coded up an example using the objects from the sample recruiting app, and I got the error that you saw, so it seems there is a bit of a disconnect. 

 

However, the following behaved as expected:

 

 

<apex:page standardController="Candidate__c"> <apex:outputField value="{!Candidate__c.Education__c}"/> <apex:outputPanel rendered="{!Candidate__c.Education__c=='High school'}"> <br/> <apex:outputText value="Should see this"/> </apex:outputPanel> <apex:outputPanel rendered="{!Candidate__c.Education__c!='High school'}"> <apex:outputText value="Shouldn't see this"/> </apex:outputPanel> </apex:page>

 

Is this any different to your experience?

 

 

 

This was selected as the best answer
dwwrightdwwright
I though that I had tried that before, but evidently not. It appears to be working now. Thanks bob