function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Anmol Verma 20Anmol Verma 20 

Lightning table to display Contacts of related accounts

I am trying to display a lightning table which contains contacts information like-
FirstName, Lastname , Email, Phone, Related Account.

I am asked to use wrapper class in apex controller. So, the issue that i am facing is that, I am getting recordID as Null.

here is the code that i am using for this task.

AssignComponent.cmp   -- Component
<aura:component controller='AccountsController' implements="force:appHostable,flexipage:availableForAllPageTypes,force:hasRecordID" access="global">
    <aura:handler name='init' value='{!this}' action ='{!c.doInit}'/>
    <aura:Attribute name='wrapperClass' type ='object'/>
    <aura:attribute name='contactList' type='List'/>
    
    <div class='slds-p-around--large'>
    	<h1 style='font-size:25px;'> Details of Contacts </h1>
    
    
    <table class='slds-table slds-table--bordered slds-table--cell-buffer'>
    	<thead>
        	<tr class='slds-text-title--caps'>
                <th scope='col'>
                	<div class='slds-truncate' title='First name'> First Name</div>
                </th>
                <th scope='col'>
                	<div class='slds-truncate' title='Last name'> Last Name</div>
                </th>
                <th scope='col'>
                	<div class='slds-truncate' title='Phone Number'> Phone Number</div>
                </th>
            </tr>
        </thead>
        <tbody>
        	<aura:iteration items="{!v.contactList}" var="con">
                <lightning:recordViewForm recordId="{!contact.Id}" objectApiName="Contact">
            	<th scope="row">
                	<div class="slds-truncate" title="{!con.FirstName}"></div>
                </th>
                <th scope="row">
                	<div class="slds-truncate" title="{!con.LastName}"></div>
                </th>
                <th scope="row">
                	<div class="slds-truncate" title="{!con.Phone}"></div>
                </th>
                </lightning:recordViewForm>
            </aura:iteration>
        </tbody>
    </table>
    </div>

</aura:component>

AssignComponentController.js ---- JS Controller
({
      doInit: function(component, event, helper) {
        // Fetch the account list from the Apex controller
        console.log('Inside Controller doInit method');
        helper.getAccountListHelper(component,event,helper);
        console.log('After helper call');
      }
    })

AssignComponentHelper.js -- Helper Method
({
      // Fetch the accounts from the Apex controller
      getAccountListHelper: function(component,event,helper) {
          console.log("Inside helper getAccountListHelper method");
          //call apex class method
          var action = component.get("c.getContacts");
          var a= component.get("v.recordID");
          //Here the recordID is getting null value//

          console.log("Record ID is: "+a);
          
          action.setParams({
              acctId : a
              
          });
          
          action.setCallback(this, function(response) {
              console.log('Inside setcallback method of helper');
              var state = response.getState();
              if(state === "SUCCESS"){
                  //set the wrapperClass attribute of component
                  //component.set('v.wrapperClass',response.getReturnValue());
                  var contactList = response.getReturnValue();
                  component.set("v.contactList",contactList);
                  console.log('Contact List>>>>'+contactList);
              console.log('values set successfully');
              }
          });
   		console.log('Exiting helper');
        $A.enqueueAction(action);
      }
    })

AccountController.apxc  ---- Apex class
public class AccountsController {
    @AuraEnabled
    public static List<Contact> getContacts(List<ID> acctId) {
    	WrapperClass  wc = new WrapperClass();
        List<Contact> contactList = [ select id ,firstName, lastName, phone
                                 from contact
                                where AccountID in :acctID];
         wc.conList=contactList;
        return contactList;
    }
    
    public class WrapperClass{
        @AuraEnabled
        public List<Contact> conList {get;set;}
        @AuraEnabled
        public Account accRecord {get; set;}
    }
        
      
}

AssignComponentApp.app
<aura:application extends="force:Slds">
    <c:AssignComponent/>
</aura:application>

The output on the console

Kindly Help me know where i am getting the things wrong.

Any help will be appreciated
Priyananth RPriyananth R
Hi Anmol,

Record Id is only visible for a sObject type.
Record Id doesn't Support in standalone app.

Please integrate your component in Account Sobject using App bulider.

Thanks,
Suraj Tripathi 47Suraj Tripathi 47
Hi Anmol,
"In this way you can display account and related contact in a table."
<aura:component controller="DisplayAccountWithContact" implements="flexipage:availableForAllPageTypes" access="global">
    <aura:handler name="init" value="{!this}" action="{!c.doInit}"/>
    <aura:attribute name="accounts" type="List" />
    <table>
        <tr>
            <td>
                <b>Name</b>
            </td>
            <td>
                <b>Phone</b>
            </td>
            <td>
                <b>Contacts</b>
            </td>
        </tr>
        <aura:iteration items="{!v.accounts}" var="acc" >
            <tr>  
                <td> {!acc.Name}  </td>
                  <td> {!acc.Phone}  </td>
                 <!--   <td>   {!acc.Contacts.lastName}  </td> -->
                <table>
                    <aura:iteration items="{!acc.Contacts}" var="con" >
                        <tr>
                            <td>{!con.LastName}</td>
                        </tr>
                    </aura:iteration>
                </table>
            </tr> 
         </aura:iteration>                                           
    </table>    
</aura:component>
If you find your Solution then mark this as the best answer. 

Thank you!

Regards 
Suraj Tripathi