+ Start a Discussion
Chase MaldonadoChase Maldonado 

Counting Child Record data for use with LWC

Hello, I am having trouble finding a way to pull aggregate data from the below Apex class that I have for a component being built. We need to record the count of Id's and Total units from the child record ChildAccounts. I am relatively new to coding and so any information and help would be greatly appreciated!
 
public with sharing class GetAccountContactData {
    @AuraEnabled(cacheable=true)
    public static List<Account> getAccountData() {
        return [SELECT Id, Name, 
        (SELECT Id, Name, Email, Phone, Title FROM Contacts), 
        (SELECT Id, Name, Region_picklist__c, Total_Units__c FROM ChildAccounts) 
        FROM Account WHERE Active__c = TRUE AND RecordTypeId = '0122K000001Dio1QAC' ORDER BY Name];
    }
}

 
Best Answer chosen by Chase Maldonado
Suraj Tripathi 47Suraj Tripathi 47

Hi

You can take references from the below code.

<aura:component controller="accountsWithContactsClass" implements="flexipage:availableForAllPageTypes" access="global">
    <aura:handler name="init" value="{!this}" action="{!c.myAction}"/>
    <aura:attribute name="accounts" type="Account[]" />
    <table>
        <tr>
            <td>
                <b>Name</b>
            </td>
            <td>
                <b>Industry</b>
            </td>
            <td>
                <b>Contacts</b>
            </td>
        </tr>
        <aura:iteration items="{!v.accounts}" var="accs1" >
            <tr>  
                <td> {!accs1.Name}  </td>
                  <td> {!accs1.Industry}  </td>
                 <!--   <td>   {!accs1.Contacts.lastName}  </td> -->
                <table>
                    <aura:iteration items="{!accs1.Contacts}" var="con1" >
                        <tr>
                            <td>{!con1.LastName}</td>
                        </tr>
                    </aura:iteration>
                </table>
            </tr> 
         </aura:iteration>                                           
    </table>    
</aura:component>
 
({
	myAction : function(component, event, helper) {
		var action =component.get("c.getAllAccounts");
        console.log('The action value is: '+action);
         action.setCallback(this, function(a){ 
             
            component.set("v.accounts", a.getReturnValue());
           //  console.log('The accs are :'+JSON.stringify(a.getReturnValue()));
            console.log('The accs are :'+JSON.stringify(a.getReturnValue()));
          
        });
        $A.enqueueAction(action);
	}
})

 

public class accountsWithContactsClass {

@auraEnabled
public static list<account> getAllAccounts()
    {
       list<account> accs =[select id,name,phone,industry,(select lastName from contacts) from account limit 10];
      //  list<account> accs =[select id,name,phone,industry from account limit 10];
     //   return [select Id,Name from account limit 10];
     return accs;
    }
}


Please mark it as the Best Answer if your queries are solved.

Thank You

All Answers

SUCHARITA MONDALSUCHARITA MONDAL

Hi Chase,

You can use the JSON.stringify(data) (data return from Apex Controller) and then iterate them. 

I'm putting snippet for similar use case.

 

Apex Class :

@AuraEnabled(cacheable=true)
    public static List<Account>getAccountRelatedData(){
        System.debug('--- Related Data---');
        return[SELECT Id, Name, Industry, Type, AnnualRevenue,
               (SELECT Id, Name FROM Contacts) FROM Account  WHERE NumberOfEmployees > = 50 ];
               
    }
 





JS Controller (LWC)
 

import { LightningElement, wire, track } from 'lwc';
import getAccountRelatedData from '@salesforce/apex/AccountController.getAccountRelatedData';
export default class AccountControllerLWC extends LightningElement {
    @track accounts;
    @track error;
    @wire(getAccountRelatedData)
    wiredAccounts({ error, data }) {
        if (data) {
            this.accounts = data;
            console.log('getAccountRelatedData-->'+JSON.stringify(this.accounts));
        } else if (error) {
            console.log(error);
            this.error = error;
        }
    }
}


Hope this will give some idea.

 

Thanks!

Sucharita

Suraj Tripathi 47Suraj Tripathi 47

Hi

You can take references from the below code.

<aura:component controller="accountsWithContactsClass" implements="flexipage:availableForAllPageTypes" access="global">
    <aura:handler name="init" value="{!this}" action="{!c.myAction}"/>
    <aura:attribute name="accounts" type="Account[]" />
    <table>
        <tr>
            <td>
                <b>Name</b>
            </td>
            <td>
                <b>Industry</b>
            </td>
            <td>
                <b>Contacts</b>
            </td>
        </tr>
        <aura:iteration items="{!v.accounts}" var="accs1" >
            <tr>  
                <td> {!accs1.Name}  </td>
                  <td> {!accs1.Industry}  </td>
                 <!--   <td>   {!accs1.Contacts.lastName}  </td> -->
                <table>
                    <aura:iteration items="{!accs1.Contacts}" var="con1" >
                        <tr>
                            <td>{!con1.LastName}</td>
                        </tr>
                    </aura:iteration>
                </table>
            </tr> 
         </aura:iteration>                                           
    </table>    
</aura:component>
 
({
	myAction : function(component, event, helper) {
		var action =component.get("c.getAllAccounts");
        console.log('The action value is: '+action);
         action.setCallback(this, function(a){ 
             
            component.set("v.accounts", a.getReturnValue());
           //  console.log('The accs are :'+JSON.stringify(a.getReturnValue()));
            console.log('The accs are :'+JSON.stringify(a.getReturnValue()));
          
        });
        $A.enqueueAction(action);
	}
})

 

public class accountsWithContactsClass {

@auraEnabled
public static list<account> getAllAccounts()
    {
       list<account> accs =[select id,name,phone,industry,(select lastName from contacts) from account limit 10];
      //  list<account> accs =[select id,name,phone,industry from account limit 10];
     //   return [select Id,Name from account limit 10];
     return accs;
    }
}


Please mark it as the Best Answer if your queries are solved.

Thank You

This was selected as the best answer