+ Start a Discussion
Michael3.BrownMichael3.Brown 

How Can I Calculate Subtotals Based upon the Records in my Object.

Hello, I followed the tutorials to create a VisualForce page that lists all the records on my object:

 

 

    public DisplayMarginEscalators[] getMarginEscalators()
    {
    
        if (marginescalatorsHigh == null)
        {
            marginescalatorsHigh = new DisplayMarginEscalatorsHigh[]{};
        
            for (Pipeline_Tracker__c pt : [SELECT Subcategory__c, Account__c,        
                 Sales_Pole__c, Probability__c, Q1_Revenue__c, SD_Owner__c,
FROM Pipeline_Tracker__c WHERE Year__c = 2011]) { marginescalators.add(new DisplayMarginEscalators(pt)); } } return marginescalators; }

 However, what I want to do is see if I can create another method that sets up a loop to go through my record collection and collect a total amount for the object: Q1_Revenue__c. I'm not sure how to do this, though.

I've been playing around with a for loop:

 

Integer x;
Integer total;
public Integer getTotal()
{
   for (x=0;x<marginescalators.size();x+1)
   {
       total = total + marginescalators[x].Q1_Revenue__c;
   }
}

 

But, I'm having trouble getting it working. Does anyone have any insight into how I can collect the running total for this amount? Please note, that I cannot use a SUM function because I have already used up my limit for SOQL queries.

MotokiMotoki

Hi there,

 

Have you forgott your return statement in the getTotal method? And please use x++ instead of x+1 and also don't put your variables outside of that method unless you have a good reason for that.

 

Best Regards,

Motoki

Michael3.BrownMichael3.Brown

Hi Motoki,

 

I added the return statement and changed x+1 to x++, but the error seems to be lyinig in the code:

 

total = total + marginescalators[x].Q1_Revenue__c

 

I am getting an error saying the Variable Q1_Revenue__c does not exist. 

 

So, it seems I'm incorrectly referencing the field I'm trying to add.  In the code above, I'm using x to try and point to a specific record, and then Q1_Revenue_c is the specific field I want to add to the total . Any suggestions on what else I can try?

 

Thanks,

Mike

MotokiMotoki

So the variable marginescalators is a List of DisplayMarginEscalators objects. Is DisplayMarginEscalators a inner Apex Class or It is a custom object?

 

Or actually you want a to count the Q1_Revenue__c field of a list of Pipeline_Tracker__c objects instead?

Michael3.BrownMichael3.Brown

Hi Motoki

 

DisplayMarginEscalators was it's own class, but I mistakenly believe it was a list. I went back and basically set up a list called marginescalators, into which I poured all my object records. Doing this allowed me to calculate the total then, by using total = total + marginescalators.Q1_Revenue__c;

 

That same formula won't work when marginescalators represented an instance of the class DisplayMarginEscalators.

 

Thanks for all the help and feedback.

 

- Mike