0

I am trying to create a final variable in apex controller which would store the value of a picklist field , example - 'Hello'.

I have a visualforce page where I need to use this final variable to compare and see whether the apex custom link should be rendered or not.

VF Page piece of code looks like:

    <apex:commandLink value="XYZ" rendered="{!AND( var.boolean__c &&  var.picklistfield__c!='Hello')}">

</apex:commandLink>

I am using the picklist value directly here which makes it like hardcoding. Is this ok to use it this way. The link should be rendered only if the picklist value has 'Hello'. I was wondering if it is ok to use 'Hello' directly in the VF page like above. If not I plan to use a final variable in apex controller to be used in VF page. But that final variable will also have a value hardcoded in apex controller.

Controller: Below is not the correct way of declaring a final variable in apex class. I use get, set to be able to access the pickVal in VF page. I get an compile error - Description Resource Path Location Type Save error: Methods are final by default, Use virtual to declare methods that can be overriden .cls

public class AController{

public final pickVal{
get{
 return 'Hello';
}
private set;
}

What do you suggest. This picklist value check is not going to change in future(very less likely). It will be 'Hello' for long time and won't change.

4

If you want the variable to be final, you can expose the getter as a method instead of using the shorthand syntax:

final String myVariable;
public String getMyVariable() { return myVariable(); }

However, there's not much risk in just using the syntax you have without making it final. It can't be set from the page because of private set, so the intention is quite clear.

public String myVariable { get; private set; }

Note that if you define a body for your get, you have to include a return statement. Instead you can just not define a body, and the standard behavior is to return the value as you would expect.

| improve this answer | |
  • Thanks. Do you think I can go with putting the pickval in the VF page itself. Basically I have show that link only if that particular value is present. – Arnold Dec 18 '17 at 21:40
  • 2
    Your code as written in the question does not even reference this property. That said, I don't see any problem with referencing the field directly. You don't get any benefit from involving Apex code. – Adrian Larson Dec 18 '17 at 21:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.