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
Dhanalakshmi Vellachamy 3Dhanalakshmi Vellachamy 3 

Lightning component datatable to show the fields from Sub Query

I am trying set up a lightning component on the Account that will show the contact and what is the most recent activity associated with it. I tried using the Data table and now I am bale to get the contact records in the lightning component.User-added imageBut I am not sure how to include the subject and OwnerId from the Activities histories in to the Datatable column. Below is the Component and Aura enabled apex class

Apex Class
public with sharing class accountSummaryLightningComponent {
    
    @AuraEnabled
public static list<Contact> getRelatedList(Id recordId)
{
        
List<Contact> Conlist = [Select id,firstname,lastname,Contact_Salutation_First_and_Last_Name__c,(SELECT Id,Subject FROM ActivityHistories ORDER BY ActivityDate DESC LIMIT 1) from Contact where AccountId =: recordId and Key_contact__c = true];
return Conlist;
}
}

Aura component
 
<aura:component controller = "accountSummaryLightningComponent" implements="flexipage:availableForRecordHome,force:hasRecordId" access="global" >
    <aura:attribute name="recordId" type="Id" />
    <aura:attribute name="ContactList" type="Contact[]"/>
    <aura:attribute name="columns" type="List"/>
        
    <aura:handler name="init" value="{!this}" action="{!c.myAction}" />
    <lightning:card iconName="standard:work_capacity_usage" title="Key Contacts">
        <aura:if isTrue="{!not(empty(v.ContactList))}">
            <lightning:datatable data="{!v.ContactList }" 
                         columns="{!v.columns }" 
                         keyField="Id"
                         hideCheckboxColumn="true"/>           
            <aura:set attribute="else">
                <div Style="text-align : center"> " There are no key contacts " </div>
            </aura:set>
        </aura:if>
    </lightning:card>
</aura:component>

Controller
 
({
    myAction : function(component, event, helper) 
    { 
        component.set('v.columns', [
            {label: 'Contact Name', fieldName: 'linkName', type: 'url',
             typeAttributes: {label: { fieldName: 'Contact_Salutation_First_and_Last_Name__c' }, target: '_blank'}}
        ]);
        
        var ConList = component.get("c.getRelatedList");
        ConList.setParams
        ({
            recordId: component.get("v.recordId")
        });
        
        ConList.setCallback(this, function(data) 
        {          
            var state = data.getState();
            if (state === "SUCCESS") {
                var records =data.getReturnValue();
                records.forEach(function(record){
                    record.linkName = '/'+record.Id;
                });
                 component.set("v.ContactList", data.getReturnValue());            
            }
        });
        $A.enqueueAction(ConList);
    }
})

Can anyone please help how to include the Subject and Owner of the Activity in the datatable​​​​​​​
CharuDuttCharuDutt
Hii  Dhanalakshmi Vellachamy

Copy Paste The Below Code.
({
    myAction : function(component, event, helper) 
    { 
        component.set('v.columns', [
            {label: 'Contact Name', fieldName: 'linkName', type: 'url',
             typeAttributes: {label: { fieldName: 'Contact_Salutation_First_and_Last_Name__c' }, target:               '_blank'}},
         {label: 'Activity', fieldName: 'Subject',type:'text'},
         {label: 'OwnerId', fieldName: 'OwnerId'}
        ]);
        
        var ConList = component.get("c.getRelatedList");
        ConList.setParams
        ({
            recordId: component.get("v.recordId")
        });
        
        ConList.setCallback(this, function(data) 
        {          
            var state = data.getState();
            if (state === "SUCCESS") {
                var records =data.getReturnValue();
                records.forEach(function(record){
                    record.linkName = '/'+record.Id;
                    record.Subject = record.ActivityHistories ? record.ActivityHistories[0].Subject : '';
                });
                 component.set("v.ContactList", data.getReturnValue());            
            }
        });
        $A.enqueueAction(ConList);
    }
})


Apex

public with sharing class accountSummaryLightningComponent {
    
    @AuraEnabled
public static list<Contact> getRelatedList(Id recordId)
{
        
List<Contact> Conlist = [Select id,firstname,lastname,Contact_Salutation_First_and_Last_Name__c,OwnerId,(SELECT Id,Subject FROM ActivityHistories ORDER BY ActivityDate DESC LIMIT 1) from Contact where AccountId =: recordId and Key_contact__c = true];
return Conlist;
}
}
Please Don't Forget Mark it As Best Answer If it Helps.
Thank You!