+ Start a Discussion
mwoodallmwoodall 

How to access AggregateResult object values in Visualforce

I have a simple extention that sums a particular contact field for all contacts of an account.  Here is the method extracted from the extention class:

 

    public AggregateResult getSalesTotal() {
        AggregateResult salesTotal =
            [SELECT SUM(Contact.Num_Orders__c) total
            FROM Contact];
        return salesTotal;

    }

 

Can I access the 'total' field from within Visual force?  I was expecting that {!SalesTotal.total} would work.

 

I know how to get the value within the Apex class method, but would rather pass the whole object and extract the fields using Visualforce.

 

Thanks.

Kevin042Kevin042

Did you ever get this to work?

mwoodallmwoodall

Not exactly -- since I couldn't figure out how to access the AggregateResult object within the page, I just created a key/value list of all the totals I needed within the extention and then accessed the list from the page.  I got the functionality I needed, just a couple extra steps:

 

  // Get all the sum data in one query
  public AggregateResult contactSumResults = 
      [SELECT SUM(Contact.Num_Orders__c) no,
        SUM(Contact.Num_Orders_Last_7_days__c) nols,
        SUM(Contact.Number_OQFs__c) noqf,
        SUM(Contact.OQFs_last_7_days__c) noqfls,
        SUM(Contact.Dollarss_Submitted__c) nd,
        SUM(Contact.s_Submitted_Last_7_days__c) ndls
      FROM Contact
      WHERE Contact.AccountId = :ApexPages.currentPage().getParameters().get('id')];
  
  
  public List<ContactSums> contactSumsList = new List<ContactSums>();
  
  public List<ContactSums> getContactSumsList() {
    // Map of sum names and pretty description
    Map<String, String> sumKeyNames = new Map<String, String>();
    sumKeyNames.put('no', '# of Orders'); // Num_Orders__c
    sumKeyNames.put('nols', '# of Orders in Last 7 Days'); // Num_Orders_Last_7_days__c
    sumKeyNames.put('noqf', '# of OQFs'); //Number_OQFs__c
    sumKeyNames.put('noqfls', '# of OQFs in Last 7 Days'); //OQFs_last_7_days__c
    sumKeyNames.put('nd', '$s Submitted'); //Dollarss_Submitted__c
    sumKeyNames.put('ndls', '$s Submitted in Last 7 Days'); //s_Submitted_Last_7_days__c

    for (String key : sumKeyNames.keySet()) {
      ContactSums cs = new ContactSums();
      cs.sumKey = key;
      cs.sumDesc = sumKeyNames.get(key);
      cs.sumValue = (Double) contactSumResults.get(key);
      contactSumsList.add(cs);
    }
    return contactSumsList; 
  }