0

How can I return a List<AggregateResult> to a lightning component and display the individual values in the lightning component. I have a table, and at the bottom of the table I need to display the totals for the columns. When I debug the response.getReturnValue() it comes across as "object object" but I can't seem to get the helper to get the values. Only uploaded code relevant to this topic. This is what I have so far:

Lightning Component:

<aura:component controller="FOS_LegExpenseTable_Helper" implements="flexipage:availableForAllPageTypes" access="global" >
   
   <aura:handler name="init" value="{!this}" action="{!c.doInit}"/>
   <aura:attribute name="PDTotal" type="Decimal"/>
   <aura:attribute name="PDDays" type="Integer"/>

   <div class="slds-scrollable" style="height:50rem">
        <!-- Crew and ICFA Per-Diem -->
        <table class="slds-table slds-table_bordered slds-table_striped slds-table_cell-buffer slds-table_fixed-layout">
          <thead>
            <tr style="line-height:20px;" >
              <th scope="col" width="15%"><div class="slds-truncate slds-cell-wrap" title="Contract #">Crew and ICFA Per-Diem</div></th>
              <th scope="col" width="20%"><div class="slds-truncate slds-cell-wrap" title="Change Order #">Crew #</div></th>
              <th scope="col" width="20%"><div class="slds-truncate slds-cell-wrap" title="JDE Line Number">PD Days</div></th>
              <th scope="col" width="20%"><div class="slds-truncate slds-cell-wrap" title="Amount">PD Rate</div></th>
              <th scope="col" width="20%"><div class="slds-truncate slds-cell-wrap" title="Amount">PD Total</div></th>
            </tr>
          </thead>
          <tbody>
            <!-- Use the Apex model and controller to fetch server side data -->
            <aura:iteration items="{!v.CrewAndICFAList}" var="leg" indexVar="idx">
                <c:FOS_LegExpenseTableData showSaveCancelBtn="{!v.showSaveCancelBtn}" singleRec="{!leg}" recordTypeNumber="1" index="{!idx}"/>   
            </aura:iteration>
          </tbody>
          <tbody>
              <td scope="col" width="15%">Totals</td>
              <td scope="col" width="20%"></td>
              <td scope="col" width="20%">{!v.PDDays}</td>
              <td scope="col" width="20%"></td>
              <td scope="col" width="20%">{!v.PDTotal}</td> ********************
          </tbody>
        </table>
</div>
</aura:component>

Controller:

doInit : function(component, event, helper) 
{
   helper.loadTotals(component, event);
}

Helper:

loadTotals : function(component, event) 
    {
        var action = component.get('c.getTotals');
        action.setParams({"fosID" : component.get("v.recordId")});
        action.setCallback(this, function(response)
        {
            var state = response.getState();
            if(state === "SUCCESS")
            {
                var map = response.getReturnValue();
                var pdt = map[1].get('PDTotal');
                alert(pdt);
                component.set('v.PDTotal', pdt);
            }
        });
        $A.enqueueAction(action);
    },

Apex:

public class FOS_LegExpenseTable_Helper
{
    @auraEnabled
    public static List<AggregateResult> getTotals(String fosId)
    {
        List<Id> legIds= new List<Id>();
        for(stack__QuoteLeg__c leg :  [Select id, name from stack__QuoteLeg__c where stack__Quote__c =: fosID])
        {
            //build list of leg ids
            legIds.add(leg.id);
        }
        List<AggregateResult> fieldTotals = [select SUM(PD_days__c) PDDays, SUM(PD_Total__c) PDTotal 
                                             from Leg_Expense__c where quote_leg__c in: legIds];
                
        return fieldTotals;
    }
}
9
  • any reason why you are developing using Aura instead of LWC ?
    – glls
    Jul 6, 2021 at 19:11
  • Yeah I think this would be easier in LWC - and you could still use your inner Aura Component. However, I'd say best thing to do for now is to serialize the returned value in the @AuraEnabled method. Then just JSON.parse it in the aura app. I see that there is no attribute to hold the iterated result, but I assume that's just because you didn't include it in the code snippet? Jul 6, 2021 at 19:15
  • not a strong javascript developer. more familiar with aura. I'm open to ideas though
    – ThatGuy
    Jul 6, 2021 at 19:16
  • @CasparHarmer I'm trying to store the iterated result in the aura attributes
    – ThatGuy
    Jul 6, 2021 at 19:19
  • 1
    @theNewGuy that means it is returning nothing to aura helper method from apex class. can you try to system.debug in your apex method to see if there are any records before the return statement. Jul 6, 2021 at 19:25

2 Answers 2

2

I think the issue is with the params you are sending from your helper to the apex class. I see "fosID" is in quotes and is not the same as in your apex class fosId

try this and I believe it will solve your issue.

action.setParams({fosId : component.get("v.recordId")});

As you are receiving this response [{"PDDays":13, "PDTotal": 237}] that means the values are at zero index of the array.

To assign the values to your individual aura attributes you can get the values like below.

loadTotals : function(component, event) 
    {
        var action = component.get('c.getTotals');
        action.setParams({fosId : component.get("v.recordId")});
        action.setCallback(this, function(response)
        {
            var state = response.getState();
            if(state === "SUCCESS")
            {
                var map = response.getReturnValue();
                var pdt = map[0].PDTotal;
                alert(pdt);
                component.set('v.PDTotal', pdt);
            }
        });
        $A.enqueueAction(action);
    },

If you expect more then one value in an array for example [{"PDDays":13, "PDTotal": 237}, {"PDDays":22, "PDTotal": 137}, {"PDDays":26, "PDTotal": 337}] you can iterate over the response array and grand total the values to set them to the aura attribute.

var map = response.getReturnValue();
var pdt = 0;
for(let i=0; i<map.length;i++){
    pdt += map[i].PDTotal;
}
component.set('v.PDTotal', pdt);
5
  • thanks, cant believe i missed this. smh. I can get data back now but still having trouble assigning individual values to variables
    – ThatGuy
    Jul 6, 2021 at 19:47
  • @theNewGuy can you please share the debug response you are receiving now. Jul 6, 2021 at 19:49
  • [{"PDDays":13, "PDTotal": 237}]
    – ThatGuy
    Jul 6, 2021 at 19:53
  • @theNewGuy i have updated the solution. please have a look. Jul 6, 2021 at 20:03
  • you forgot the var readMap = JSON.stringify(map); also alert(pdt) is coming across as "undefined".. Your "to assign values" is literally the same exact code that i posted....that doesn't work.
    – ThatGuy
    Jul 8, 2021 at 15:56
0

Using this I was able to get the individual values from the AggregateResult.

loadTotals : function(component, event) 
    {
        var action = component.get('c.getTotals');
        action.setParams({"fosId" : component.get("v.recordId")});
        action.setCallback(this, function(response)
        {
            var state = response.getState();
            if(state === "SUCCESS")
            {
                var map = response.getReturnValue();
                var deserialize = JSON.stringify(map);
                alert(deserialize);
                map.forEach(function(item){
                    component.set('v.PDTotal', item.PDTotal);
                    component.set('v.PDDaysTotal', item.PDDaysTotal); 
            }
            else
            {
                alert("Error loading Record Total values");
            }
        });
        $A.enqueueAction(action);
    },

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .