Monday, January 27, 2014

Access custom controller-defined enum in custom component

Access custom controller-defined enum in custom component

We cannot reference the enum directly since the enum itself is not visible to the page and you can’t make it a property.
Example:
Apex class:
global with sharing class My_Controller {
  public Case currCase {get; set; }
  public enum StatusValue {RED, YELLOW, GREEN}

  public StatusValues getColorStatus() {
    return StatusValue.RED;  //demo code - just return red
  }
}

Visualforce page:
<apex:image url='stopsign.png' rendered="{!colorStatus == StatusValue.RED}" />

Above code snippet will throw error something like “Save Error: Unknown property ‘My_Controller.statusValue’”

Resolution:
Add below method in Apex Controller:
public String currentStatusValue { get{ return getColorStatus().name(); }}
and change Visualforce code to
<apex:image url='stopsign.png' rendered="{!currentStatusValue == 'RED'}" />

The same question was raised in this thread of stackExchange
  

No comments:

Post a Comment