function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion

How Do I Call a Public Void Method on my VisualForce page?

First, let me apologize if this is a dumb question. I'm trying to reach back and pull my college Java courses, but I'm struggling.


Anyways, I have a public void method that I want to query a custom object, and depending on a field value, add each record to a certain list. I am doing this because I have multiple lists I will be calling from my VisualForce page, so I have the public void method to build the lists, and then functions to return those lists. However, when I call my lists, none of them are populated with data, so it seems my public voide method is not ran.


Here is the public void method:

public class MarginEscalators
List<Pipeline__c> marginEscalatorsComplete = new List <Pipeline__c>();
List<Pipeline__c> marginEscalatorsIncomplete = new List <Pipeline__c>();

public void buildLists()
        for (Pipeline__c me2011 : [SELECT      
                 ProjectID__C, Sales_Pole__c, Status__C
        FROM Pipeline__c          
        WHERE Year__c = 2011)
                if (me2011.Status__c == 'Complete')
                else if (me2011.Status__c == 'Incomplete')


 Here are my two simple functions:

public List<Pipeline__c> getMarginEscalatorsComplete()
    return marginEscalatorsComplete;

public List<Pipeline__c> getMarginEscalatorsIncomplete()
    return marginEscalatorsIncomplete;



I assumed that whenever I called either {!marginEscalatorsComplete} or {!marginEscalatorsIncomplete} on my VF page my public void method would be kicked off automatically. Is there something else I need to do in order to initiate the public void method?


The code below is how I've tried calling just one of my lists to see if my code worked, but it's not. I'm not getting errors, but I am getting a blank page.

<apex:repeat value="{!marginEscalatorsComplete}" var="a">       
     <TR><TD width="75"><apex:outputText value="{!a.Subcategory__c}" /></TD>
         <TD width="100">{!a.ProjectID__c}</TD>
         <TD width="40">{!a.Sales_Pole__c}</TD>
         <TD width="75">{!a.Status__c}</TD></TR>


Again, I think it's because my public void method is not get initiated and building the lists, and I'm not sure how to make sure it is run. If anyone has any insight, I would greatly appreciate it.




Best Answer chosen by Admin (Salesforce Developers) 

Doesn't seem to be anything in your code to call it.  


Perhaps you just need to add a constructor to do the initialization?


public marginEscalators() {





All Answers


Doesn't seem to be anything in your code to call it.  


Perhaps you just need to add a constructor to do the initialization?


public marginEscalators() {





This was selected as the best answer

Thanks aballard!!!! That was it! That's all I needed, haha.


Hi Mike.


I saw your post, and had a small sample that I use to demo this kind of functionality to my students when I teach Visualforce.  Though you already have a solution, I thought you might find it useful so will post below.


Here's the page, similar to yours except it uses the Visualforce PageBlockTable to automatically iterate the list and generate an HTML table, instead of having to use a Repeat component.


It also demonstrates loading the list from a command button using Ajax to call the controller's action method, and does a partial rerender of just the output panel containing the table.  


When the form first loads, it doesn't show any data, but refreshes the list upon clicking the button. 



name        : VFDemo_LoadList
author      : Don Robins (
date        : 11th February, 2011
description : Demo page to show drag drop in action
<apex:page controller="VFDemo_LoadListController" showHeader="false" sidebar="false" >
    <apex:sectionHeader title="List Demo Page" subtitle="Load a list of Accounts"/>
    <apex:form >
        <apex:commandButton action="{!refreshList}" value="Click To Reload the List" reRender="listArea" id="btnLoad" /><br/>

    <apex:outputPanel id="listArea">
        <apex:pageBlock title="Short List Of Accounts">
            <apex:pageBlockTable value="{!accountList}" var="a">
                <apex:column value="{!}"/> 
                <apex:column value="{!}"/> 


Here's the controller code; note that there are numerous places where you could load the list from when the page first loads, including the constructor (the solution cited above), or something called a static initializer which fires before the constructor, or in the get{}; accessor if you were to use a public property rather than a public getter method for the list. You could of course also put the same logic in the getter method too.


You'll see that a call to the private buildList() method can be wrapped in the public action method refreshList(); this is the action method bound to the CommandButton on the view, and it returns a null PageReference object so that navigation returns to the same view when called. It's job is simply to perform the partial rerender of the outputPanel content.



name        : LoadListController
author      : Don Robins (
date        : 11th February, 2011
description : Controller to drive a demo load list page
note        : Note that WITH SHARING is not active
public class VFDemo_LoadListController{

    //Private member variable scoped only within the constructor, 
    //exposed through the public getter method or public property. 
    private List<Account> accounts = new List <Account>();

    //    buildList();

    //Default constructor
    public VFDemo_LoadListController(){
        //You can load the lists in the constructor so the are preloaded when the page inits.		

    //Getter method to return a populated list from the private member variable. 
    public List<Account> getAccountList(){
        //Assumed to be loaded.
        return accounts;

    //The getter method above could be replaced with a public readonly property instead, 
    //public List<Account> AccountList{
    //    get{
    //        //Calls the buildList() method if the private member variable is not yet loaded.
    //        if (accounts == null)
    //		{
    //            buildList();
    //        }
    //        //Else returns the populated list.
    //        return accounts;
    //    }
    //Action method called from view.
    public pagereference refreshList(){
        //Return null to stay on same page.
        return null;

    //Here's the private method used to load the list; 
    //could be called from constructor or action method
    private void buildList()
        accounts = [ SELECT Id, Name FROM Account LIMIT 10 ];          

    //Don't forget to build your tests, and they should include some real asserts!
    private static void testthis(){
        VFDemo_LoadListController cont = new VFDemo_LoadListController();


And finally there's a simple unit test; it's a great practice to get in the habit of writing your unit test code as you build your classes so you don't have to worry about them later.


Hope this helps explain a few more of the potential moving parts of the Visualforce framework.








Hi Don. Great example! Thanks for sharing!