2

I am trying to create a lightning component to display a single number which is the sum or count of a power of 1 field. Found solutions to display a list of aggregate but not an unique number. Below my codes that do not work. The page displays but not the aggregate sum. Any idea?

APEX

public class RenewalsAPEXController {

@AuraEnabled        
public static AggregateResult getRenewals(){      

    return [SELECT SUM(OpportunitiesCount__c) renewals FROM Opportunity WHERE ((RecordTypeId = '012280000005zPW') and (StageName != 'Closed Won' or StageName != 'Closed Lost') and (OwnerId = :UserInfo.getUserId()))];
}      
}

Component

<aura:component controller="RenewalsAPEXController" implements="flexipage:availableForAllPageTypes" access="global" >

<aura:attribute name="renewals" type="integer"/>    
<aura:handler name="init" value="{!this}" action="{!c.doInit}" />

<!-- Use the Apex model and controller to fetch server side data -->  
<article class="slds-card">
    <header class="slds-card__header slds-grid">
      ...
    </header>   
    <div class="slds-card__body">           
        <ui:outputNumber value="{!v.renewals}" />  
    </div>     
</article>        
</aura:component>  

Controller

({
doInit: function(component) {
    var action = component.get("c.getRenewals");

    action.setCallback(this, function(a) {
        component.set("v.renewals", a.getReturnValue());            
    });
    $A.enqueueAction(action);
}
})

Thank you in advance for your help. Sylvie

1

Use integer type in your return type in apex , the apex controller can be modified as below .The aggregate object might not be supported as return type

@AuraEnabled        
public static Integer getRenewals(){      

   AggregateResult[] groupedResults = [SELECT SUM(OpportunitiesCount__c)renewals FROM Opportunity WHERE ((RecordTypeId = '012280000005zPW') and (StageName != 'Closed Won' or StageName != 'Closed Lost') and (OwnerId = :UserInfo.getUserId()))];
   Integer sum = (Integer)groupedResults[0].get('renewals');
   return sum;
}
| improve this answer | |
  • Thank you Mohith for your quick response, but it is still not displaying anything. – ultima67 Aug 17 '17 at 3:27
  • can you put a console log for a.getReturnValue() and see what value you get . – Mohith Shrivastava Aug 17 '17 at 3:29
  • Also turn on the debug logs in apex and see if the value in logs is fine – Mohith Shrivastava Aug 17 '17 at 3:31
  • I found the problem. I have to changed Integer by Decimal as a type and it works. Thank you so much for your guidance. – ultima67 Aug 17 '17 at 4:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.