1

It's meant to pull the recordId from the url of a users record page and use it to display a list of that users open opportunities. Everything is working without a hitch, except the account names aren't being displayed. Opportunity name and stage name are working fine. And when I debug, the SOQL is indeed grabbing the account names, the table just isn't displaying them

Here's my code

.cmp

<aura:component controller="MyOpenOpportunitiesController" implements="flexipage:availableForAllPageTypes,force:hasrecordId" access="global" >
    <aura:handler name="init" value="{!this}" action="{!c.doInit}" />
    <aura:attribute name="recordId" type="Id" />
    <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': 'Account.Name', 'type': 'text'}
    ]" />


    <lightning:card iconName="standard: user" title="{!v.cardTitle}">
        <lightning:datatable data="{! v.Opportunities }" columns="{! v.Columns }" keyField="Id" hideCheckboxColumn="true"/>
    </lightning:card>
</aura:component>

.js controller

({
    doInit : function(component, event, helper) {
        Promise.all([helper.fetchUser(component), helper.fetchOpportunities(component)])
            .then($A.getCallback(() => helper.setCardTitle(component)));
    }})

.js helper

({
  fetchUser: function (component) {
    return new Promise((resolve, reject) => {
        var action = component.get("c.getUser");
        action.setParams({
            userId: component.get("v.recordId")
        });

        action.setCallback(this, function (response) {
            var state = response.getState();
            if (state === "SUCCESS") {
                component.set("v.user", response.getReturnValue());
                resolve();
            } else if (state === "ERROR") {
                var errors = response.getError();
                console.error(errors);
                reject(errors);
            }
        });

        $A.enqueueAction(action);
    });
},

fetchOpportunities: function (component) {
    return new Promise((resolve, reject) => {
        var action = component.get("c.getOpenOpportunities");
        action.setParams({
            recordId: component.get("v.recordId")
        });

        action.setCallback(this, function (response) {
            var state = response.getState();
            if (state === "SUCCESS") {
                component.set("v.Opportunities", response.getReturnValue());
                resolve();
            } else if (state === "ERROR") {
                var errors = response.getError();
                console.error(errors);
                reject(errors);
            }
        });

        $A.enqueueAction(action);
    });
},
setCardTitle: function (component) {
    var action = component.get("c.getCurrentUserId");
    action.setCallback(this, $A.getCallback(function (response) {
        var state = response.getState();
        if (state === "SUCCESS") {
            var currentUserId = response.getReturnValue();
            var recordId = component.get("v.recordId");
            var recordUserName = component.get("v.user.Name");
            var condition = (currentUserId === recordId);
            var title = condition ? "My Open Opportunities" : recordUserName + "'s Open Opportunities";
            component.set("v.cardTitle", title);
        } else if (state === "ERROR") {
            var errors = response.getError();
            console.error(errors);
        }
    }));
    $A.enqueueAction(action);
  }
})

.cls controller

public with sharing class MyOpenOpportunitiesController {
    // Add a property to store the open opportunities for the specified user
    public List<Opportunity> openOpportunities {get; set;}

    public MyOpenOpportunitiesController() {
        // Retrieve the 'recordId' parameter from the URL
        String recordId = ApexPages.currentPage().getParameters().get('recordId');
        // Get the open opportunities for the specified user
        openOpportunities = getOpenOpportunities(recordId);
    }

    @AuraEnabled
    public static String getCurrentUserId() {
        return UserInfo.getUserId();
    }

    @AuraEnabled
    public static User getUser(String userId) {
        return [SELECT Name FROM User WHERE Id = :userId];
    }
@AuraEnabled
public static List<Opportunity> getOpenOpportunities(String recordId) {
    List<Opportunity> opportunities = new List<Opportunity>();
    if (recordId != null) {
        System.debug('Record ID: ' + recordId); // Log the recordId
        opportunities = [SELECT Id, Name, StageName, Account.Name FROM Opportunity WHERE OwnerId = :recordId AND IsClosed = false];
        System.debug('Number of opportunities fetched: ' + opportunities.size()); // Log the number of opportunities fetched
    } else {
        System.debug('Record ID is null'); // Log a message if recordId is null
    }
    return opportunities;
}
}
6

0

You must log in to answer this question.

Browse other questions tagged .