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

Trouble displaying aggregate results in visualforce page
I'm brand new to Apex and I'm having some trouble displaying aggregate results in a visualforce page. The extension isn't throwing any errors but the visualforce says "Error: Invalid field cv__Opportunity__c for SObject AggregateResult".
The way I read this is, either I'm not calling the fields properly in the visualforce page, or I'm not presenting the aggregate properly in the extension. Any help is of course greatly appreciated!
Here's the extension:
public class AccountDesignationHistoryExtension { Account CurrentAccount = null; List<AggregateResult> AccountDesignationHistory = null; public AccountDesignationHistoryExtension(ApexPages.StandardController controller) { CurrentAccount = (Account)controller.getRecord(); AccountDesignationHistory = [SELECT COUNT(cv__Opportunity__c), Designation_Name__c, SUM(cv__Amount__c), MAX(Close_Date__c) From cv__Donation_Designation_Relationship__c WHERE cv__Opportunity__r.AccountId = :CurrentAccount.Id AND cv__Opportunity__r.StageName = 'Received' AND cv__Opportunity__r.cv__Posted__c = False GROUP BY Designation_Name__c]; } public List<AggregateResult> getAccountDesignationHistory() { return AccountDesignationHistory; } }
Here's the visualforce:
<apex:page standardController="Account" extensions="AccountDesignationHistoryExtension">
<apex:pageBlock >
<apex:pageBlockTable value="{!AccountDesignationHistory}" var="a">
<apex:column value="{!a.cv__Opportunity__c}" headerValue="Number of Donations" />
<apex:column value="{!a.Designation_Name__c}" headerValue="Designation Name" />
<apex:column value="{!a.cv__Amount__c}" headerValue="Total Designated Amount" />
<apex:column value="{!a.Close_Date__c}" headerValue="Most Recent Donation" />
</apex:pageBlockTable>
</apex:pageBlock>
</apex:page>
Try testing the query in Force.com Explorer. Look's like some issue with your query.
hmm...The error shows it is balking at the cv__Opportunity__c field. This field has an aggregate function applied to it. I assume you need to either provide the field an alias or refer to it using the general notation for aggregated fields?
(see more info here) (<- 8/16 4:30: updated link to right section of developer docs...)
options:
1) provide the aggregated field an alias in the query & use that alias on the page
then do something like <apex:column value="{!a.myCount}" headerValue="Number of Donations" />
2) Don't provide an alias and just use the default expression
<apex:column value="{!a.expr0}" headerValue="Number of Donations" />
I find option 1 more desirable because it provides a known handle to get a specific value, AND if you change the order of the aggregated fields in the query at some point in the future you don't have to update the visualforce (in case 'aggregated field #1 becomes the third aggregated field in the query....etc) - as the default expressions depend on the order of aggregation
let me know if that works!
I'd love to be able to use that solution, but it doesn't work either. I pasted the modified code snippets below. I'm still getting the "invalid field" error on the visualforce page, except now it's saying TotalGifts is an invalid field, instead of cv__Opportunity__c.
A former colleague, who is an Apex wizzard, suggested I take a hint from either Jeff Douglas or Scott Hemmeter. These may be some simple modifications, but I couldn't figure out what to do. Like I said, I'm brand new at this; no coding background whatsoever. Thanks for your reply.
<apex:column value = "${!a['TotalGifts']}" /> should help ..
First, give an alias name to all the columns that you are fetching Next, access them in the Visual force page as
For example:
Hope this helps :D
Cheers.
Hi,
I have this problem
Case_Product__c - my junction table(I have many-to-many between Product and Case).
It is my component names 'CasesListForBoss':
<apex:component controller="CasesController" access="global">
<apex:dataTable value="{!ListCasesForBoss}" var="case">
<apex:column >
<p>Case Number: {!case['CaseNumber']}
Owner: {!case['OwnerFirstName']} {!case['OwnerLastName']}
Status: {!case['CaseStatus']} Product:{!case['ProductName']}</p>
</apex:column>
</apex:dataTable>
</apex:component>
CasesController:
public class CasesController {
public List<AggregateResult> getListCasesForBoss() {
List<AggregateResult> results = Database.query('SELECT Product__r.Name ProductName,'+
'Case__r.CaseNumber CaseNumber,'+
'Case__r.Status CaseStatus,'+
'Owner.FirstName OwnerFirstName,'+
'Owner.LastName OwnerLastName'+
'FROM Case_Product__c WHERE CreatedDate = LAST_N_DAYS:7');
return results;
}
}
Email template:
<messaging:emailTemplate subject="Case_Product__c" recipientType="User" relatedToType="Case_Product__c">
<messaging:htmlEmailBody >
<p>As you requested, here is a list of all our cases</p>
<c:CasesListForBoss />
<p>Enjoy!</p>
</messaging:htmlEmailBody>
</messaging:emailTemplate>
Please,help me