component:
<aura:component controller="AcctOppsController" implements="flexipage:availableForRecordHome,force:hasRecordId">
<aura:attribute name="mydata" type="OpportunityContactRole[]"/>
<aura:attribute name="mycolumns" type="List"/>
<aura:attribute name="recordId" type="Id" />
<aura:attribute name="currentRecordId" type="Id" />
<aura:handler name="init" value="{!this }" action="{! c.doInit }"/>
<div style="height: 300px">
<lightning:datatable
keyField="id"
data="{! v.mydata }"
columns="{! v.mycolumns }"
hideCheckboxColumn="true"/>
</div>
</aura:component>
controller:
({
doInit: function (cmp, event, helper) {
cmp.set('v.mycolumns', [
{label: 'Opportunity Name', fieldName: 'opportunityId', type: 'text'},
{label: 'Contact Name', fieldName: 'contact', type: 'text'},
{label: 'Role', fieldName: 'role', type: 'text'},
{label: 'Amount', fieldName: 'amount', type: 'currency'}
]);
var fetchData = {
opportunityId: "opportunityId",
contact : "Contact.Name",
role : "Role",
amount : "Opportunity.Amount"
};
helper.fetchData(cmp,fetchData);
}
})
helper:
({
fetchData : function(cmp) {
var recordId = cmp.get("v.recordId");
var action = cmp.get("c.getOpps");
action.setParams({ "currentRecordId" : recordId });
action.setCallback(this, $A.getCallback(function (response) {
var state = response.getState();
if (state ==="SUCCESS") {
cmp.set("v.mydata", response.getReturnValue());
} else if (state === "ERROR") {
var errors = response.getError();
console.error(errors);
}
}
));
$A.enqueueAction(action);
}
})
apex controller:
public with sharing class AcctOppsController{
@AuraEnabled
public String currentRecordId {get;set;}
public AcctOppsController(ApexPages.StandardController controller) {
currentRecordId = ApexPages.CurrentPage().getparameters().get('id');
}
@AuraEnabled
public List<OpportunityContactRole> getOpps() {
List<OpportunityContactRole> oppresults = [SELECT Contact.name, Role, OpportunityId, Opportunity.Amount, Opportunity.StageName, Opportunity.Type FROM OpportunityContactRole WHERE contact.accountid=:currentRecordId];
return oppresults;
}
}
Any thoughts on what I am missing?
Hi,With a new object : let ob = {}; it is simpler. ({
readData : function(cmp) {
var recordId = cmp.get("v.recordId");
var action = cmp.get("c.myOpps");
action.setParams({ "currentRecordId" : recordId });
action.setCallback(this, $A.getCallback(function (response) {
var state = response.getState();
var res = [];
if (state ==="SUCCESS") {
var resp = response.getReturnValue();
console.log(resp);
resp.forEach(function(item, index, array) {
let ob = {};
ob.opportunityName = item.Opportunity.Name;
ob.contact = item.Contact.Name;
ob.role = item.Role;
ob.amount = item.Opportunity.Amount;
res.push(ob);
});
cmp.set('v.mydata',res);
} else if (state === "ERROR") {
var errors = response.getError();
console.error(errors);
}
}
));
$A.enqueueAction(action);
},
})
({
doInit: function (cmp, event, helper) {
cmp.set('v.mycolumns', [
{label: 'Opportunity Name', fieldName: 'opportunityName', type: 'text'},
{label: 'Contact Name', fieldName: 'contact', type: 'text'},
{label: 'Role', fieldName: 'role', type: 'text'},
{label: 'Amount', fieldName: 'amount', type: 'currency'}
]);
helper.readData(cmp);
}
})
<aura:attribute name="mydata" type="Object[]"/> : generic array of objectspublic with sharing class AcctOppsController{
@AuraEnabled
public static List<Object> myOpps(String currentRecordId) {
List<OpportunityContactRole> oppresults = [SELECT Contact.name, Role, OpportunityId, Opportunity.Amount,Opportunity.Name, Opportunity.StageName, Opportunity.Type FROM OpportunityContactRole WHERE contact.accountid=:currentRecordId];
return oppresults;
}
}
<aura:component controller="AcctOppsController" implements="flexipage:availableForRecordHome,force:hasRecordId">
<aura:attribute name="mydata" type="Object[]"/>
<aura:attribute name="mycolumns" type="List"/>
<aura:attribute name="recordId" type="Id" />
<aura:attribute name="currentRecordId" type="Id" />
<aura:handler name="init" value="{!this }" action="{! c.doInit }"/>
<div style="height: 300px">
<lightning:datatable
keyField="id"
data="{! v.mydata }"
columns="{! v.mycolumns }"
hideCheckboxColumn="true"/>
</div>
</aura:component>