I looked at this and this before posting this. I don't understand the responses so much as to tweak them to fit into my needs, so any pointers in the right direction would be great.

So: Say there is a building object that has multiple 'expenses' records (master-detail relationship). Needless to say that the number of expenses per building could vary (electricity, phone, internet, cleaning... etc) Every expense has an escalation rate every year.

What I am trying to do is to generate a report per building like so:
Month Expense1 Expense2 Expense3 ... MonthTotal
1/1/2015 $100 $150 $200 ... SUM
2/1/2015 $100 $150 $200 ... SUM
1/1/2016 $100+increase $150+increase $200+increase ... SUM

If there were going to be a finite number of expenses, I know I could just create a data structure and display the report using List. But how do I do this when the number of expenses is varying?

EDIT: How to add a new column for cumulative sum? Like so:

Month Expense1 Expense2 Expense3 ... MonthTotal CUMULATIVE TOTAL
1/1/2015 $100 $150 $200 ... sum1 SUM1
2/1/2015 $100 $150 $200 ... sum2 (SUM2 + SUM1)
1/1/2016 $100+increase $150+increase $200+increase ... sumX (SUMX + SUM(X-1) + SUM(X-2) +... + SUM2 + SUM1)

  • Ok so to boil it down you basically want to create an <apex:pageBlockTable> with a dynamic amount of columns?
    – Force Hero
    Jan 30, 2015 at 16:35
  • Yes Sir.......! Jan 30, 2015 at 16:38
  • I'm having issues accessing my test instance (grr) so this is untested but can you do this? (see answer for formatted code)
    – Force Hero
    Jan 30, 2015 at 16:42
  • Mind assisting with the controller as well? I am having trouble understanding how the apex:repeat works. Salesforce documentation doesn't do a good job explaining it. I mean, how is this displaying a variable number of columns? Jan 30, 2015 at 16:55
  • Yeah sure, i'm on the road atm so I'll have a look when I'm back at my machine
    – Force Hero
    Jan 30, 2015 at 17:33

1 Answer 1


VF Page:

<apex:page controller="DynoListTest" action="{!SetupData}">

    <style type="text/css">
        .empty-column-content {
            display: none;

    <apex:pageBlock >
        <apex:pageBlockTable value="{!Wraps}" var="wrap">
            <apex:column value="{!wrap.Name}" headerValue="Header 1" /> <!-- This would be the Opp name or any other Opp fields -->
            <apex:repeat value="{!wrap.exps}" var="exp">
                <apex:column value="{!exp.Amount}" headerValue="This doesn't render" /> <!-- This is the expense name or any other expense field -->

            <apex:repeat var="header" value="{!headers}">
                <apex:column headerValue="{!header}" styleClass="empty-column-content" />


public with sharing class DynoListTest {

    public List<Wrapper> Wraps {get; set;}
    public List<String> Headers {get; set;}

    public void SetupData() {

        List<Expense> exps = new List<Expense>();
        exps.add(new Expense('Expense 1', 100));
        exps.add(new Expense('Expense 2', 200));
        exps.add(new Expense('Expense 3', 300));

        Wraps = new List<Wrapper>();
        Wraps.add(new Wrapper('Test1', exps));

        exps = new List<Expense>();
        exps.add(new Expense('Expense 1', 400));
        exps.add(new Expense('Expense 2', 500));
        exps.add(new Expense('Expense 3', 600));

        Wraps.add(new Wrapper('Test2', exps));

        Headers = new List<String>();
        for (Expense exp : exps) {

    public class Wrapper {
        public String Name {get; set;}
        public List<Expense> Exps {get; set;}

        public Wrapper(String pName, List<Expense> pExps) {
            Name = pName;
            Exps = pExps;

    public class Expense {
        public String Name {get; set;}
        public Decimal Amount {get; set;}

        public Expense(String pName, Decimal pAmount) {
            Name = pName;
            Amount = pAmount;


I hit an issue that while pageBlockTables do support the apex:repeat tag with columns inside the column headers don't show up. Fortunately this guy has already resolved that by having a separate list of strings that contain the headers. We then render them after the actual columns and hide the cells it produces using CSS.

  • Exactly what I wanted! I now understand how this works! Thank you! Jan 30, 2015 at 21:20
  • Awesome! Glad I could help.
    – Force Hero
    Jan 30, 2015 at 21:25
  • I couldn't figure out how to add to your reputation... can you tell? Jan 30, 2015 at 21:45
  • Yeah because you marked it as the best answer I got 25 pts :)
    – Force Hero
    Jan 30, 2015 at 22:51
  • As an extension to this, how could we calculate the cumulative sum for each expense and display it in new column? Feb 6, 2015 at 16:44

You must log in to answer this question.

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