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
Travis Malle 9Travis Malle 9 

Get method with SOQL query not working

I have a visualforce page to display some basic account data. The page uses the standard Account controller with a custom extension. Basically I just want the page to display the sum of all the related donations (Donation__c) via  {!TotalDonations }.
My custom controller is intended to get this information using a get method and SOQL query but I just can’t get it to work. I’ve tried several variations of code but have been unsuccessful. If anyone can help point me in the right direction it would be greatly appreciated.
 
 
 
 
 
public with sharing class MyTabbedExtension {
public MyTabbedExtension(ApexPages.StandardController controller) {
}
       
public integer totaldonations(){
return database.query(SELECT Sum(Donation_Amount__c) FROM Donation__c WHERE Parent_Account__c = :ApexPages.currentPage().getParameters().get('ID'));
}
 // THIS WAS JUST ANOTHER ATTEMPT THAT DID NOT WORK  
Public list <Donation__c> getTotalDonations(){    
return totalDonations = [SELECT Sum(Donation_Amount__c) FROM Donation__c WHERE Parent_Account__c = :ApexPages.currentPage().getParameters().get('ID')];
}
   
 
}
Best Answer chosen by Travis Malle 9
Abhishek BansalAbhishek Bansal
Hi Travis,

Please remove all methods and add the below mention method :
 
public Integer getTotalDonations(){
    AggregateResult[] groupedResults = [SELECT Sum(Donation_Amount__c) FROM Donation__c WHERE Parent_Account__c = :ApexPages.currentPage().getParameters().get('ID')];
    Integer totalDonation = Integer(groupedResults[0].get('expr0'));
    return totalDonation;
}

Let me know if you face any issue with this.

Regards,
Abhishek Bansal.

All Answers

Muthuraj TMuthuraj T
Hi,

https://developer.salesforce.com/docs/atlas.en-us.soql_sosl.meta/soql_sosl/sforce_api_calls_soql_select_agg_functions.htm
This document will give you the details about SOQL aggregate functions. Please go through it. If you didn't get answer, please paste your VF page code here.
Abhishek BansalAbhishek Bansal
Hi Travis,

Please remove all methods and add the below mention method :
 
public Integer getTotalDonations(){
    AggregateResult[] groupedResults = [SELECT Sum(Donation_Amount__c) FROM Donation__c WHERE Parent_Account__c = :ApexPages.currentPage().getParameters().get('ID')];
    Integer totalDonation = Integer(groupedResults[0].get('expr0'));
    return totalDonation;
}

Let me know if you face any issue with this.

Regards,
Abhishek Bansal.
This was selected as the best answer