You need to sign in to do that
Don't have an account?
Jay Weerappulige
Returning aggregate values on lightning page
I have built a lightning page where I want to display summarized data from an aggregate apex method. my method has some return value problem. this is my method and I want to return Ass and Credits
my code is below
public class OutstandingCreditAuraController {
@AuraEnabled public static List<Assessment_Schedule__c> getOutstandingCredits(){
List<AggregateResult> agrResults = [SELECT Assessor_name__c ass,
Sum(Credits__c) credits
FROM Assessment_Schedule__c
WHERE (TEC_Status__c = 'Active' OR TEC_Status__c = 'On Hold') and (Company__c = 'CityFitness HO')
Group By Assessor_name__c];
for (AggregateResult ar: agrResults){
return((decimal)ar.get('credits'));
}
}
}
my code is below
public class OutstandingCreditAuraController {
@AuraEnabled public static List<Assessment_Schedule__c> getOutstandingCredits(){
List<AggregateResult> agrResults = [SELECT Assessor_name__c ass,
Sum(Credits__c) credits
FROM Assessment_Schedule__c
WHERE (TEC_Status__c = 'Active' OR TEC_Status__c = 'On Hold') and (Company__c = 'CityFitness HO')
Group By Assessor_name__c];
for (AggregateResult ar: agrResults){
return((decimal)ar.get('credits'));
}
}
}
Try to run below sample!
- In the component, a list of AssessmentQueryResult is defined and doinit method is called for initialization.
- doinit method calls getSumOfCreditList method and getSumOfCreditList returns the list of AssessmentQueryResult.
- component shows the list values by <aura:iteration>
Component:
Controller (client-side):
Controller(Apex)
Wrapper Class: In above sample, to simplify client-side controller, getSumOfCreditList returns list not map directly. <aura:iteration> is used only for list. So, when you want getSumOfCreditList return map, client-side controller will be more complex to convert map to list.
References:
http://salesforce.stackexchange.com/questions/78903/is-it-possible-to-iterate-over-a-map-in-lightning
All Answers
Return type shouled be same return() as definition of getOutstandingCredits().
In your code, getOutStandingCredits() requires List<Assessment_Schedule__c> and return() method returns decimal value.
Your requirement is returning sum values. But I think Assessment_Schedule__c doesn't have the sum field. So define a new class to return query results. Add <aura:attribute> with type AssessmentQueryResult to your lightning page.
Sorry for mistake. It will save when AssessmentQueryResult is inner class. However, inner class cannot be an attribute of lightning component. Try to save AssessmentQueryResult as top level class. (Add public keyword and save as another class)
How about using map? You can also set sums in a wrapper class like AssessmentQueryResult.
Sorry for hasseling again. I get the 'Expecting right curly bracket, found 'for'' error when compiling this piece of code line no 7 after the Map.
Also how do I get the return vales to the inner class AssessmentQueryResult
I am still learning the basic of Apex.
Thanks for your valuble support sofar.
Jay
Try to run below sample!
- In the component, a list of AssessmentQueryResult is defined and doinit method is called for initialization.
- doinit method calls getSumOfCreditList method and getSumOfCreditList returns the list of AssessmentQueryResult.
- component shows the list values by <aura:iteration>
Component:
Controller (client-side):
Controller(Apex)
Wrapper Class: In above sample, to simplify client-side controller, getSumOfCreditList returns list not map directly. <aura:iteration> is used only for list. So, when you want getSumOfCreditList return map, client-side controller will be more complex to convert map to list.
References:
http://salesforce.stackexchange.com/questions/78903/is-it-possible-to-iterate-over-a-map-in-lightning
jay