0

I am trying to display a pageBlockTable with my AggregateResult and then have a total in the footer of a column. I found a possible answer here, however the code gives me an error.

The error appears on the last line of my Apex. It is saying that the '=', ')', and the ";" on that last line are unexpected tokens. I ran this exact code block in execute anonymous (adding in system.debug and substituting an actual Id) and it ran with no issue.

Here is the Apex:

public class julAugPromos {

Public list<AggregateResult> japrom;
public julAugPromos(ApexPages.StandardController stdController){
    japrom = [select report_description__c RDesc, SUM(qty_shipped__c) Quantity, SUM(Total_Sale_Amount__c) Total 
              from invitem__c 
              where cust_num__c = :ApexPages.currentPage().getParameters().get('id')
              and sales_cat__c = 'PROM' 
              and inv_date__c >= 2017-06-15 
              and inv_date__c <= 2017-08-14 
              Group by report_description__c
              Order by SUM(Total_Sale_Amount__c) desc];
}
public list<AggregateResult> japromlist {
    get { return japrom;}
}

public double endtotal{get;set;}

   AggregateResult[] tot 
       = [select SUM(Total_Sale_Amount__c)Gtot from invitem__c 
           where cust_num__c = :ApexPages.currentPage().getParameters().get('id') 
           and sales_cat__c = 'PROM' 
           and inv_date__c >= 2017-06-15 
           and inv_date__c <= 2017-08-14];
Object sumAmount = tot [0].get('Gtot');
endtotal=(double)sumAmount;

}

And here is the VF:

<apex:page standardController="Account" tabStyle="Account" extensions="julAugPromos">
<apex:pageBlock title="Jul-Aug 2017 Promos" tabStyle="Account">
    <apex:pageBlockSection columns="1">
        <apex:pageBlockTable value="{!japromlist}" var="o">
            <apex:column >
                <apex:facet name="header">Product Description</apex:facet>
                {!o['RDesc']}</apex:column>
            <apex:column >
                <apex:facet name="header">Qty Sold</apex:facet>
                    {!o['Quantity']}</apex:column>
            <apex:column >
                <apex:facet name="footer">{!endTotal} </apex:facet>
                <apex:facet name="header">Total Sales</apex:facet>
                {!o['Total']}</apex:column>
        </apex:pageBlockTable>
    </apex:pageBlockSection>
</apex:pageBlock>

Any help would be greatly appreciated!

2 Answers 2

2

You can't put executable code outside of a method body; you can put declarations with their assignments, but you can't generally execute whatever code you like. Ideally, this means you should move your code to within the constructor:

public class julAugPromos {

Public list<AggregateResult> japrom;
public julAugPromos(ApexPages.StandardController stdController){
    japrom = [select report_description__c RDesc, SUM(qty_shipped__c) Quantity, SUM(Total_Sale_Amount__c) Total 
              from invitem__c 
              where cust_num__c = :ApexPages.currentPage().getParameters().get('id')
              and sales_cat__c = 'PROM' 
              and inv_date__c >= 2017-06-15 
              and inv_date__c <= 2017-08-14 
              Group by report_description__c
              Order by SUM(Total_Sale_Amount__c) desc];

   AggregateResult[] tot 
       = [select SUM(Total_Sale_Amount__c)Gtot from invitem__c 
           where cust_num__c = :ApexPages.currentPage().getParameters().get('id') 
           and sales_cat__c = 'PROM' 
           and inv_date__c >= 2017-06-15 
           and inv_date__c <= 2017-08-14];
Object sumAmount = tot [0].get('Gtot');
endtotal=(double)sumAmount;

}
public list<AggregateResult> japromlist {
    get { return japrom;}
}

public double endtotal{get;set;}

}
2
  • This makes sense! Quick follow up, hopefully to help me understand better. If I instantiate the variable endtotal after the constructor, why is it able to be assigned a new value inside the constructor? Is this due to the order of operations? Thanks for your help, Brian!
    – Brio87
    Commented Aug 30, 2017 at 13:37
  • @Brio87 Yes, while most examples you'll see place all of the variables at the top of the class, it does not matter where they appear, as they will be accessible anywhere within the class. Within a method, however, you must declare a variable before you can use it.
    – sfdcfox
    Commented Aug 30, 2017 at 15:22
0

Following code must be used inside getter section of endtotal, which currently outside of any methods or properties or constructor, that's why the error is.

AggregateResult[] tot 
       = [select SUM(Total_Sale_Amount__c)Gtot from invitem__c 
           where cust_num__c = :ApexPages.currentPage().getParameters().get('id') 
           and sales_cat__c = 'PROM' 
           and inv_date__c >= 2017-06-15 
           and inv_date__c <= 2017-08-14];
Object sumAmount = tot [0].get('Gtot');
endtotal=(double)sumAmount;
2
  • I would discourage this method generally, because getters may be executed more than once, consequently slowing down the page and possibly even violating governor limits. Always cache your query results so you do not execute them more than once per transaction.
    – sfdcfox
    Commented Aug 29, 2017 at 22:10
  • That's really good to know Commented Aug 29, 2017 at 22:11

You must log in to answer this question.

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