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
Wes BarnesWes Barnes 

AggregateResult usage in VF page...

Hi all --

 

I have a VF page and corresponding controller extension that I am using to display parent and child account information.  I am trying to total a rollup summary field at the child account level using an aggregateresult function.  Here is the VF page detail:

 

 

<apex:pageBlockSection title="Account Detail">
   <apex:outputText value="Account Name: "/><apex:outputText value="{!account.name}"/>
   <apex:outputText value="Account Type: "/><apex:outputText value="{!account.type}"/>
   <apex:outputText value="Institution Type: "/><apex:outputText value="{!account.Institution_Type__c}"/>      
   <apex:outputText value="Total Spend: "/><apex:outputText value="{!TotalParentSpend}"/>
   <apex:outputText value="Account Owner: "/><apex:outputText value="{!account.owner.name}"/>
    <apex:outputText value="Last Update: "/><apex:outputText value="{!account.LastModifiedDate}"/>
</apex:pageBlockSection>

 

 

and the Controller Code:

 

 

Public AggregateResult getTotalParentSpend (){
  return [Select SUM(Total_Spend__c) from account where parentid= :System.currentPageReference().getParameters().get('id') and (type = 'Customer')];
		}

 

 

The page and controller will save in the IDE but when I go to render the page I get an "Internal Server Error"

 

I am assuming that this is because the VF page is not rendering the data for that type of aggregate result but I cannot figure out how to get my sum value onto the page.  Any help is appreciated.

 

Thanks,


Wes

aballardaballard

You can't use aggregate results directly in VF currently, so you ened to use Apex to return the selected elements.   In your case, change getTotalParentSpend to something like

Public Integer getTotalParentSpend (){
  AggregateResult agg = [Select SUM(Total_Spend__c) total from account
      where parentid= :System.currentPageReference().getParameters().get('id') and (type = 'Customer')];
  return agg.get('total');
}

 

 

 

 

 

 

 

Wes BarnesWes Barnes

aballard --

 

Thanks for the prompt reply I appreciate it as I did not know that I need to return the specific elements!  I have just tried this however I am getting an error:  "Return value must be of type: Integer"  There is obviously a type mismatch on what I am declaring and what is being returned but I am not sure what to set it to.  I have tried, double, integer, decimal, and aggregateresult none of which have worked.  If it helps the field that is being totaled is of type double.

 

Thanks again,


Wes

aballardaballard

What is the actual field type definition of the custom field?  There is no field type of Double. 

Wes BarnesWes Barnes

It is a rollup summary field.