Displaying the records from multiple related Salesforce Objects on a Lightning Web Component.

Author - Webner

The records from a single Salesforce Object can simply be displayed in a Lightning Web Component by using the lightning datatable, but in order to display the records from multiple objects on the same Lightning Page, we need to use the Wrapper Class in Apex and Aura Iteration (<aura:iteration>) in the component.

Note: Make sure the multiple objects we’re going to fetch the records from, are related to each other because we’ll need a related field that will play an important role in fetching the records from its parent object.

Step 1: Let’s begin with creating the Apex Class!

We’ll need to use the Wrapper Class to return the list of records from Policy Object and Accounts, its Parent Object. Note that we have an “Account__c” field that relates the Policy with its Account Name but instead of “__c” we’ll use “__r” so we can fetch the Account Name from the Accounts Object.

public class TestWrapperClass {
public static wrapperClass getList(){
wrapperClass returnwrapperClass = new wrapperClass ();
String soql = 'Select Name,Account__r.Name from Policy__c';
List<Policy__c> PoList= Database.Query(soql);
if(PoList != null && PoList.size()>0){
List <Policy__c> returnList = new List <Policy__c> ();
for (Policy__c c: PoList) {
returnwrapperClass.listPol = returnList;
return returnwrapperClass;
else{return null;}
// wrapper or Inner class with @AuraEnabled {get;set;} properties*
public class wrapperClass{
@AuraEnabled public List<Policy__c> listPol{get;set;}

Step 2: Creating the Lightning Component to display the records.

We’ll be using the Aura: Iteration tag to iterate through the list of the records and then displaying them in a table.

<aura:component controller="TestWrapperClass" implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,forceCommunity:availableForAllPageTypes,force:lightningQuickAction" access="global" >
<aura:attribute name="PoList" type="Policy__c[]"/>
<aura:handler name="init" action="{!c.doInit}" value="{!this}" />
<div class="dataTable">
<table class="slds-table slds-table--bordered slds-table--cell-buffer" >
<tr class="slds-text-title--caps ">
<th scope="col">
<span class="slds-truncate" title="Policy Name">Policy Name</span>
<th scope="col">
<span class="slds-truncate" title="Account Name">Account Name</span>
<aura:iteration items="{!v.PoList}" var="pol">
<th scope="row">
<div class="slds-truncate" style="margin-left:8px" title="{!pol.Policy_Name__c}">{!pol.Name}</div>
<th scope="row">
<div class="slds-truncate" style="margin-left:8px" title="{!pol.Account__r.Name}">{!pol.Account__r.Name}</div>

Step 3: Creating the JS controller for the component.

This will fetch the list of records that are being returned by the apex class.

doInit : function(component, event, helper) {
var action = component.get("c.getList");
action.setCallback(this, function(response) {
var state = response.getState();
if(state === 'SUCCESS'){
var result = response.getReturnValue();
console.log('Result ', result.listPol);
component.set("v.PoList", result.listPol);

Now that we’ve fetched the records from our org using the Wrapper Class and Iterated the list of records in our Component, we just need to call our Component in a Lightning Application.

Step 4 : Calling the Component in a Lightning App.

<aura:application extends="force:slds">

After fetching the records, our lightning app will look like this:
sf-table Objects

Webner Solutions is a Software Development company focused on developing Insurance Agency Management Systems, Learning Management Systems and Salesforce apps. Contact us at dev@webners.com for your Insurance, eLearning and Salesforce applications.

Leave a Reply

Your email address will not be published. Required fields are marked *