Performing Comparisons on Custom Labels in VF pages

By Vincent on February 2, 2011

Ran into a strange “bug” today while developing a VF page.

This VF receives parameters through the URL which the VF Controller class parses out and stores in a class variable. The VF page also has an <apex:outputPanel> section which is rendered if the URL parameter equals a specified Custom Label. The code looks like the following:

Example URL:
https://na#.salesforce.com/apex/MyPage?param1=paramValue

Example Custom Label Definition:
Custom Label Name : MyLabel
Custom Label Value : paramValue1

Example Visualforce Page:


<apex:page controller="MyController">
 <apex:outputPanel rendered="{!ctrlParam1=='$Label.MyLabel'}">
  <apex:outputText value="Hello, World!"/>
 </apex:outputPanel>
</apex:page>

Example Controller Class:


public class MyClass{
 public String ctrlParam1{get; private set;}
 public MyClass(){
  map<string> params = ApexPages.currentPage().getParameters();
  ctrlParam1= params.get('param1');
 }
}

With the configuration details setup as above, the <apex:outputPanel> is not rendered. More specifically, the code rendered="{!ctrlParam1=='$Label.MyLabel'}" evaluates to false.

Playing around a bit with this code, and a lot of head scratching, netted me the following code that does work:

rendered="{!ctrlParam1==$Label.MyLabel}"

(No quotes around the Custom Label). I guess $Label.MyLabel is a variable in and of itself, and not a replacement string as I assumed that it was. Wish Salesforce.com Help and Training had more sample code. Grrr..

Leave a Reply

Add a Comment »

Force.com Development