0

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.But 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

<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>

JS 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);
    }
})

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;
}
}

Can anyone please help how to include the Subject and Owner of the Activity in the datatable

** EDIT**

I updated the controller as suggested below

({
    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 Subject', fieldName: 'ActivitySubject', type: 'text'}
        ]);
        
        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()
                .map((row) => ({
                    linkName: '/'+row.Id,
                    ActivitySubject: row && row.ActivityHistories && row.ActivityHistories.length ? row.ActivityHistories[0].Subject : '',
                               }));
                component.set('v.ContactList', records);
            }
        });
        $A.enqueueAction(ConList);
    }})

The issue is now I see the record ID as hyperlink instead of the Contact_Salutation_First_and_Last_Name__c

enter image description here

1

As David said, you need to "flatten" the data. Also, you should not modify the results from getReturnValue directly, as this can cause problems. Always copy the object first. Here's what you'd want to do:

var records = data
    .getReturnValue()
    .map((row) => ({
        ...row,
        linkName: `/${row.Id}`,
        ActivitySubject:
            row && row.ActivityHistories && row.ActivityHistories.length ? row.ActivityHistories[0].Subject : '',
    }));
component.set('v.ContactList', records);
9
  • Thank you @sfdcfox the issue is now the hyperlink is acting little weird.. I updated the question above, can you please help me if I am missing any thing – user81642 Apr 6 at 2:11
  • @user81642 Make sure the field name is specified correctly. JavaScript is case-sensitive, so it might be a typo. – sfdcfox Apr 6 at 2:16
  • I missed to add the field in the row mapping.. Thank you – user81642 Apr 6 at 2:19
  • After making this change when I long in another profile user The table looks empty even though the uses has access to Contact and activity history records. Do you think anything else I am missing? I gave the Profiles access to these Aura enabled apex class still no change – user81642 Apr 6 at 2:22
  • @user81642 "looks empty"? As in, no rows, or rows are there but the fields are blank? – sfdcfox Apr 6 at 2:30
1

You can't use field paths or expressions in the definition of a Lightning data table like this:

fieldName: 'ActivityHistories[0].Subject'

As you're doing right now with the linkName field, you'd need to access those data elements and populate them into synthetic fields on the records. You can then access those fields by name.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.