You need to sign in to do that
Don't have an account?

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') { marginEscalatorsComplete.add(me2011); } else if (me2011.Status__c == 'Incomplete') { marginEscalatorsIncomplete.add(me2011); } } }
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.
<TABLE> <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> </apex:repeat> </TABLE>
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.
Thanks,
Mike
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() {
buildLists();
}
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() {
buildLists();
}
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.
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.
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.
Regards,
Don
Hi Don. Great example! Thanks for sharing!