I have a Engagement object with a custom field Account__c that has Name field. I want to show the name of the Account, but currently it shows the Id. How can I show the Account__c.Name rather than Id?
Apex Class Code:
public class EngagedAccountController {
@AuraEnabled
public static List<Engagement__c> getEngagedAccounts(Id oppId) {
Volunteer_Opportunity__c opp = [Select Name, Support_Required__c FROM Volunteer_Opportunity__c WHERE id = :oppId];
List<Engagement__c> engagedAccounts = [Select Name, Account__c FROM Engagement__c WHERE Volunteer_Opportunity__c = :opp.Id];
return engagedAccounts;
}
}
Lightning Component Javascript:
({
myAction : function(component, event, helper) {
component.set("v.Columns", [
{label:"Name", fieldName:"Name", type:"text"},
{label:"Account", fieldName:"Account__c", type:"text"}
]);
var action_get_engaged_accounts = component.get("c.getEngagedAccounts");
action_get_engaged_accounts.setParams({
oppId: component.get("v.recordId")
});
action_get_engaged_accounts.setCallback(this, function(data) {
component.set("v.accounts", data.getReturnValue());
});
$A.enqueueAction(action_get_engaged_accounts);
}
})
Lightning Component Component:
<aura:component controller="EngagedAccountController" implements="flexipage:availableForRecordHome,force:hasRecordId" access="global" >
<aura:attribute name="recordId" type="Id" />
<aura:attribute name="opportunity" type="Volunteer_Opportunity__c" />
<aura:attribute name="accounts" type="Engagement__c" />
<aura:attribute name="Columns" type="List" />
<aura:handler name="init" value="{!this}" action="{!c.myAction}" />
<force:recordData aura:id="accountRecord"
recordId="{!v.recordId}"
targetFields="{!v.opportunity}"
layoutType="FULL"
/>
<lightning:card iconName="standard:entitlement" title="{! 'Accounts engaged in this opportunity'}">
<lightning:datatable data="{!v.accounts}" keyField="id" columns="{! v.Columns }" hideCheckboxColumn="true"/>
</lightning:card>
</aura:component>
Best Answer
AFAIK the
fieldName
attribute oflightning:dataTable
does not support dotted expressions that cross object boundaries such asAccount__r.Name
.So the simplest fix is to add a formula field called "Account Name" of
Account__r.Name
to yourEngagement__c
object and query and reference that in the table.PS
Or use this technique.