Find your content:

Search form

You are here

Access custom controller-defined enum in custom component?

 
Share

Given a custom VF component that uses a custom controller; the controller defines an enum:

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
  }
}

In the component, I want to test using the enum value:

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

But trying to save (via Force.com IDE) responds, Save Error: Unknown property 'My_Controller.statusValue'

StatusValue is a public enum, it should be visible here. How do I reference this enum from the component?


Attribution to: Jason Clark

Possible Suggestion/Solution #1

Just a guess, but do you need to quantify where StatusValue is by prefixing it with the class name and possibly a namespace prefix?

E.g.

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

On second thought it probably isn't a namespace issue. I did find Having trouble referencing a user defined enum which suggests making a string property for the name.

From the link (with modifications to bring it inline with the question):

I don't think you can directly reference the enum since the enum itself is not visible to the page and you can't make it a property. I resolved the issue by adding in one more controller property.

public String currentStatusValue { get{ return getColorStatus().name(); }}

Then change your page to be:

<apex:image url='stopsign.png' rendered="{!currentStatusValue == 'RED'}" />

Personally, I'd look at making a property that returns a boolean directly. E.g.

public boolean renderStopSign { get{ return getColorStatus() == StatusValue.RED; }}

<apex:image url='stopsign.png' rendered="{!renderStopSign }" />

The practicality of this approach will depend on how many values you want to test for.


Attribution to: Daniel Ballinger

Possible Suggestion/Solution #2

Yeah, as Daniel suggests I don't think it's a namespace issue - I think maybe Visualforce just doesn't know what to do with the enum datatype.

So:

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

And:

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

Would work.


Attribution to: joshbirk
This content is remixed from stackoverflow or stackexchange. Please visit https://salesforce.stackexchange.com/questions/133

My Block Status

My Block Content