Skip to main content
how to display account related contact record on click of account record name in salesforce lightning?

 
1 answer
  1. Nov 6, 2018, 1:10 PM
    Hi Praveen,

    Greetings to you!

    Please try the below code, I have tested in my org and it is working fine. Kindly modify the code as per your requirement.

    Component:

    <aura:component controller="AccRelatedConC"

    implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,forceCommunity:availableForAllPageTypes,force:lightningQuickAction" access="global" >

    <aura:attribute name="PageHeading" type="String" default="Account and Related Contacts" />

    <aura:attribute name="mydata" type="List"/>

    <aura:attribute name="mydata2" type="List"/>

    <aura:handler name="init" value="{!this}" action="{!c.doinit}" />

    <div class="slds-m-top--xx-large">

    <div class="slds-page-header">

    <div class="slds-align--absolute-center">

    <div class="slds-text-heading--large">

    {!v.PageHeading}

    </div>

    </div>

    </div>

    </div>

    <br/> <br/>

    <div class="slds-section slds-is-open">

    <h3 class="slds-section__title slds-theme_shade">

    <span class="slds-truncate slds-p-horizontal_small" title="Section Title">Accounts</span>

    </h3>

    <br/>

    <table class="slds-table slds-table--bordered slds-table--striped slds-table--cell-buffer slds-table--fixed-layout">

    <thead>

    <tr class="slds-text-heading--label">

    <th scope="col"><div class="slds-truncate" title="Vehicle Name">Account Name</div></th>

    <th scope="col"><div class="slds-truncate" title="Service Date">Phone</div></th>

    </tr>

    </thead>

    <tbody>

    <aura:iteration items="{!v.mydata}" var="row" indexVar="index">

    <tr>

    <th scope="row"><div class="slds-truncate" >

    <a onclick="{!c.showCon}" value="{!row}" data-index="{!index}">{!row.Name}</a>

    </div></th>

    <td><div class="slds-truncate" title="{!row.Phone}">{!row.Phone}</div></td>

    </tr>

    </aura:iteration>

    </tbody>

    </table>

    </div>

    <br/><br/>

    <div class="slds-section slds-is-open">

    <h3 class="slds-section__title slds-theme_shade">

    <span class="slds-truncate slds-p-horizontal_small" title="Section Title">Related Contact Details</span>

    </h3>

    <br/>

    <table class="slds-table slds-table--bordered slds-table--striped slds-table--cell-buffer slds-table--fixed-layout">

    <thead>

    <tr class="slds-text-heading--label">

    <th scope="col"><div class="slds-truncate" title="Contact Name">Contact Name</div></th>

    <th scope="col"><div class="slds-truncate" title="Phone">Phone</div></th>

    <th scope="col"><div class="slds-truncate" title=""></div></th>

    </tr>

    </thead>

    <tbody>

    <aura:iteration items="{!v.mydata2}" var="row2">

    <tr>

    <th scope="row"><div class="slds-truncate" title="{!row2.Name}">{!row2.Name}</div></th>

    <td><div class="slds-truncate" title="{!row2.Phone}">{!row2.Phone}</div></td>

    </tr>

    </aura:iteration>

    </tbody>

    </table>

    </div>

    </aura:component>

    Controller:

    ({

    doinit : function(component, event, helper) {

    var action = component.get('c.fetchAcc');

    action.setCallback(this, function(response){

    var state = response.getState();

    if(state === "SUCCESS"){

    var allValues = response.getReturnValue();

    console.log("allValues--->>> " + allValues);

    component.set('v.mydata', allValues);

    }

    else if(state === "ERROR") {

    var errors = response.getError();

    if(errors){

    if(errors[0] && errors[0].message){

    console.log("Error Message: " + errors[0].message);

    }

    }

    else{

    console.log("Unknown Error");

    }

    }

    });

    $A.enqueueAction(action);

    },

    showCon : function(component, event, helper){

    var idx = event.target.getAttribute('data-index');

    console.log('idx---->>> ' + idx);

    var rowRecord = component.get("v.mydata")[idx];

    console.log('rowRecord---->>> ' + JSON.stringify(rowRecord));

    var action = component.get('c.fetchCon');

    action.setParams({recordId : rowRecord.Id});

    action.setCallback(this, function(response){

    var state = response.getState();

    if(state === "SUCCESS"){

    var allValues = response.getReturnValue();

    console.log("allValues--->>> " + JSON.stringify(allValues));

    component.set('v.mydata2', allValues);

    }

    else if(state === "ERROR") {

    var errors = response.getError();

    if(errors){

    if(errors[0] && errors[0].message){

    console.log("Error Message: " + errors[0].message);

    }

    }

    else{

    console.log("Unknown Error");

    }

    }

    });

    $A.enqueueAction(action);

    }

    })

    Apex:

    public class AccRelatedConC {

    @AuraEnabled

    public static List<Account> fetchAcc (){

    return [SELECT Id, Name, Phone FROM Account LIMIT 10];

    }

    @AuraEnabled

    public static List<Contact> fetchCon (String recordId){

    return [SELECT Id, Name, Phone FROM Contact WHERE AccountId=:recordId];

    }

    }

    I hope it helps you.

    Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future.

    Thanks and Regards,

    Khan Anas