I have created a custom object which contains multiple fields that I want to be able to show on a Visualforce page. From the discussion I've had with another developer it was recommended that I use a utility class and just reference that from the standard class. Here's what I've got.

Utility class

public without sharing class pkb_content_Utility {
  public PKB_Content__c pos{get;set;}
  public pkb_Controller() {
    pos = [SELECT Home_Forum__c FROM PKB_Content__c];

While I only reference Home_Forum__c, there are multiple other fields I need to reference as well but I am unsure of the best way to do this.

Main controller class

pos = pkb_content_Utility.pkb_Controller();

Visualforce code

<apex:page showHeader="false" standardController="KnowledgeArticle" extensions="pkb_Controller">
  <apex:outputText value="{!pos.Home_Forum__c}"/>

Adding ApexPages.StandardController sc as a constructor argument (that can be used to get a reference the KnowledgeArticle if that is needed) makes the class fit the controller extension pattern that it looks like you are aiming for based on your page:

public without sharing class PkbContentController {
      public PKB_Content__c pos {get;set;}
      public PkbContentController(ApexPages.StandardController sc) {
          // Add all the other fields you need here instead of ...
          pos = [SELECT Home_Forum__c, ... FROM PKB_Content__c limit 1];

This pattern "mixes in" the controller extension with the standard controller so your page can reference both; in your example you are only referencing the controller extension field:

<apex:page showHeader="false"
    <apex:outputText value="{!pos.Home_Forum__c}"/>
    <!-- Output more fields here -->

See e.g. the Building a Controller Extension documentation for more information.

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.