• Anmol Verma 20
  • NEWBIE
  • 0 Points
  • Member since 2019

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 0
    Replies
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