1

Maybe I'm just misunderstanding the capabilities of the Visualforce APIs and the use of functions in VisualForce but I'd like to do something like this:

Looking at the Rendered tag:

<apex:page ...>
    <apex:pageBlockSectionItem rendered="{!$Utils.checkChatterEnabled}">
        <apex:outputLabel for="enableChatterFeedTracking" value="Enable Chatter Feed Tracking" />
        <apex:outputText value="Chatter Enabled" />
    </apex:pageBlockSectionItem>
</apex:page>

And than I have a class with public or global visibility, that is NOT my controller or controller-extension:

global class Utils{
  global static Boolean checkChatterEnabled(){
    sObject feedEnabled = Database.query('SELECT Id FROM Meeting__Feed LIMIT 1');
    if(feedEnabled != null){
        return true;
    } else {
        return false;
    }
  }
}

Obviously this doesn't compile with a failure message:

Save error: Field $Utils.checkChatterEnabled does not exist

Is this possible? If so what am I doing wrong? If not, is their an "Idea" or "Success" article about it ...?

Thanks

Updated - to make checkChatterEnabled static.

1 Answer 1

2

To gain access to the functions for use in the rendered attribute (or anywhere else, for that matter), you would want to use an extension.

<apex:page standardController="Account" extensions="pageController,Util">
    <!-- code here -->
    <apex:pageBlockSectionItem rendered="{!Util.chatterFeedEnabled}">
        <!-- code -->
    </apex:pageBlockSectionItem>
</apex:page>

Your class can then be written to accept a StandardController:

public class Util {
    public Util(ApexPages.StandardController controller) { }
    public Boolean getChatterFeedEnabled() {
        ...
    }
}

You can then use this extension on multiple pages as you see fit. Note that Components are a different beast, however. If you use a component with a controller, there must also be a constructor accepting that controller as a parameter:

public Util(MyComponentController controller) { }

You can have as many constructors as you see fit (we have a multi-purpose class with eight constructors for various components).

2
  • I realize this, but I don't want to use the standard controller extension and would prefer to access the methods in the UTIL class statically to avoid code-duplication. I guess I should have specified that UTIL is a static class. Jul 22, 2013 at 19:21
  • That's the point of extensions, to avoid code duplication. Your class will only be defined once, and doesn't have to be pulled in to the page's controller. Unfortunately, anything that's not a controller or extension can't be accessed by the Visualforce runtime in an expression like that.
    – sfdcfox
    Jul 22, 2013 at 19:36

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .