I have a list of open opportunities that displays on the user profile. Here's the .cmp and the controller. The issue isn't in the js
<aura:component controller="MyOpenOpportunitiesController" implements="flexipage:availableForAllPageTypes,force:hasRecordId">
<aura:attribute name="recordId" type="String" default="{!v.recordId}" />
<aura:attribute name="Opportunities" type="Opportunity[]" />
<aura:attribute name="columns" type="List" default="[
{label: 'Opportunity name', fieldName: 'Name', type: 'text'},
{label: 'Stage', fieldName: 'StageName', type: 'text'},
{label: 'Account Name', fieldName: 'AccountName', type: 'text'}
]" />
<aura:attribute name="cardTitle" type="String" />
<aura:attribute name="user" type="User" /> <!-- Add this line -->
<aura:handler name="init" value="{!this}" action="{!c.doInit}" />
<lightning:card title="{!v.cardTitle}" iconName="standard:opportunity">
<lightning:datatable data="{!v.Opportunities}" columns="{!v.columns}" keyField="Id" hideCheckboxColumn="true" />
</lightning:card>
</aura:component>
Apex Controller
public with sharing class MyOpenOpportunitiesController {
public class OpportunityWrapper {
@AuraEnabled
public Opportunity opp {get; set;}
@AuraEnabled
public String accountName {get; set;}
public OpportunityWrapper(Opportunity opp, String accountName) {
this.opp = opp;
this.accountName = accountName;
}
}
@AuraEnabled
public static List<OpportunityWrapper> getOpenOpportunities(String recordId) {
List<OpportunityWrapper> wrappers = new List<OpportunityWrapper>();
if (recordId != null) {
System.debug('Record ID: ' + recordId);
List<Opportunity> opportunities = [SELECT Id, Name, StageName, Account.Name FROM Opportunity WHERE OwnerId = :recordId AND IsClosed = false];
for (Opportunity opp : opportunities) {
wrappers.add(new OpportunityWrapper(opp, opp.Account.Name));
}
System.debug('Number of opportunities fetched: ' + opportunities.size());
} else {
System.debug('Record ID is null');
}
return wrappers;
}
@AuraEnabled
public static String getCurrentUserId() {
return UserInfo.getUserId();
}
@AuraEnabled
public static User getUser(String userId) {
return [SELECT Name FROM User WHERE Id = :userId];
}
}
I want to make the opp names into link to that opp. I adjusted the code below, but now it just displays blanks cells in the name column (along with a fully populated table everywhere else)
<aura:component controller="MyOpenOpportunitiesController" implements="flexipage:availableForAllPageTypes,force:hasRecordId">
<aura:attribute name="recordId" type="String" default="{!v.recordId}" />
<aura:attribute name="Opportunities" type="Object[]" />
<aura:attribute name="columns" type="List" default="[
{label: 'Opportunity name', fieldName: 'oppURL', type: 'url', typeAttributes: {label: { fieldName: 'opp.Name' }, target: '_blank'}},
{label: 'Stage', fieldName: 'StageName', type: 'text'},
{label: 'Account Name', fieldName: 'AccountName', type: 'text'}
]" />
<aura:attribute name="cardTitle" type="String" />
<aura:attribute name="user" type="User" /> <!-- Add this line -->
<aura:handler name="init" value="{!this}" action="{!c.doInit}" />
<lightning:card title="{!v.cardTitle}" iconName="standard:opportunity">
<lightning:datatable data="{!v.Opportunities}" columns="{!v.columns}" keyField="Id" hideCheckboxColumn="true" />
</lightning:card>
</aura:component>
Apex Controller
public with sharing class MyOpenOpportunitiesController {
public class OpportunityWrapper {
@AuraEnabled
public Opportunity opp {get; set;}
@AuraEnabled
public String accountName {get; set;}
@AuraEnabled
public String oppURL {get; set;}
@AuraEnabled
public String accountURL {get; set;}
public OpportunityWrapper(Opportunity opp, String accountName) {
this.opp = opp;
this.accountName = accountName;
this.oppURL = '/' + opp.Id;
this.accountURL = '/' + opp.AccountId;
}
}
@AuraEnabled
public static List<OpportunityWrapper> getOpenOpportunities(String recordId) {
List<OpportunityWrapper> wrappers = new List<OpportunityWrapper>();
if (recordId != null) {
System.debug('Record ID: ' + recordId);
List<Opportunity> opportunities = [SELECT Id, Name, StageName, Account.Name, AccountId FROM Opportunity WHERE OwnerId = :recordId AND IsClosed = false];
for (Opportunity opp : opportunities) {
wrappers.add(new OpportunityWrapper(opp, opp.Account.Name));
}
System.debug('Number of opportunities fetched: ' + opportunities.size());
} else {
System.debug('Record ID is null');
}
return wrappers;
}
@AuraEnabled
public static String getCurrentUserId() {
return UserInfo.getUserId();
}
@AuraEnabled
public static User getUser(String userId) {
return [SELECT Name FROM User WHERE Id = :userId];
}
}
{ fieldName: 'opp.Name' }
. Lightning datatable and treegrid cannot navigation relationships. You must move the opportunity data to the top level of the object. In other words, you must transformopp.Name
into something that is at the top level, such asopportunityName
. You should be able to use a getter for this:@AuraEnabled public String opportunityName { get { return opp.Name; } set; }
in yourOpportunityWrapper
.