I want to display the contact names and opportunity names where AccountID is not null, I am able to display the contact names, can someone please help me to display the opportunity names too?
Below is the code I am using to display the data.
component:
<aura:component controller="OpConAccountApexController">
<aura:attribute name="ListOfData" type="Contact[]"/>
<aura:attribute name="ColumnNames" type="List" />
<aura:handler name="init" value="{!this}" action="{!c.Init}" />
<!--for columns there would be an attribute type List-->
<!--for data there would be an attribute of Sobject array -->
<lightning:datatable data="{!v.ListOfData}"
columns="{!v.ColumnNames}"
keyField="Id" />
</aura:component>
Controller:
({
Init : function(component, event, helper) {
//setting the fields of the columns
component.set('v.ColumnNames', [
{label: 'Contact Name', fieldName: 'Name', type: 'Text'},
{label: 'Opportunity Name', fieldName: 'opportunity.Name', type: 'Text'}
]);
helper.getData(component);
}
})
Helper:
({
getData : function(component) {
var action = component.get('c.getDetails');
action.setCallback(this, function(response) {
var state = response.getState();
if(state === "SUCCESS") {
//ContactList attribute of Contact array type
console.log('values: '+ response.getReturnValue());
component.set('v.ListOfData', response.getReturnValue());
}else if(state ==="ERROR") {
var error = response.getError();
console.log('Failing with the: '+ error);
}
});
$A.enqueueAction(action);
}
})
Apex Controller:
public class OpConAccountApexController {
@auraEnabled
public static List<Contact> getDetails() {
List<Contact> Contacts = [SELECT ID, Name,(SELECT Name FROM Opportunities where AccountID != null)
FROM Contact where AccountID != null];
return Contacts;
}
}