2

Hoping someone can see something that I am not able to. Myself and other users get the following error intermittently, on some records this error appears and on others it does not.

enter image description here

This LWC calls an APEX class that gets any related opportunities and will then display them onto the page for the user.

My best guess at this point is going to be in line 34 -> opportunity.Lender_Account__c.Name

<template>

    <lightning-card title={title} icon-name="standard:opportunity">

        <lightning-accordion 
            allow-multiple-sections-open 
            active-section-name={activeSections}>

            <!-- If Opportunity Data is Returned -->
            <template if:true={opportunities}>

                <!-- <ul class="slds-has-dividers_around-space"> -->
                <ul>

                    <template for:each={opportunities} for:item="opportunity">

                        <lightning-accordion-section name={opportunity.Id} key={opportunity.Id} label={opportunity.Name}>

                            <div slot="actions">
                                <lightning-button-icon icon-name="utility:info" variant="bare"
                                    alternative-text="Open record" onclick={navigateToRecord} value={opportunity.Id}
                                    data-id={opportunity.Id}>
                                </lightning-button-icon>
                            </div>

                            <div class="slds-title__detail">
                                <dl class="slds-dl_horizontal">
                                    <dt class="slds-dl_horizontal__label">Type:</dt>
                                        <dd class="slds-dl_horizontal__detail">
                                            {opportunity.Type}
                                        </dd>
                                    <dt class="slds-dl_horizontal__label">Lender:</dt>
                                        <dd class="slds-dl_horizontal__detail">
                                            {opportunity.Lender_Account__r.Name}
                                        </dd>
                                    <dt class="slds-dl_horizontal__label">Amount:</dt>
                                        <dd class="slds-dl_horizontal__detail">
                                            <lightning-formatted-number value={opportunity.Funded_Amount__c} format-style="currency" currency-code="USD"></lightning-formatted-number>
                                        </dd>
                                    <dt class="slds-dl_horizontal__label">Funded Date:</dt>
                                        <dd class="slds-dl_horizontal__detail">
                                            <lightning-formatted-date-time value={opportunity.Fund_Date__c}></lightning-formatted-date-time>
                                        </dd>

                                </dl>
                            </div>

                        </lightning-accordion-section>

                    </template>

                </ul>

            </template>
    
            <!-- If NO Opportunity Data is Returned -->
            <template if:false={opportunities}>
            </template>

        </lightning-accordion>

    </lightning-card>

</template>
import { LightningElement, wire, api, track } from 'lwc';
import getOppList from '@salesforce/apex/GetOpportunities.wonOpportunities';
import { NavigationMixin } from 'lightning/navigation';

export default class AccountWonOpportunities extends NavigationMixin(LightningElement)  {

    
    @api recordId;
    @api objectApiName;
    activeSections; //Default Open Accordion Sections 
    title;

    opportunities;
    error;
    wiredOpportunitiesResult;

    @wire(getOppList, {recordId: '$recordId'})
    opportunitiesResult(result){

        console.log('Get Funded Opportunities Result:');
        console.log(result);
        console.log(result.data);

        var opportunityRecords = [];
        var count = 0;

        this.wiredOpportunitiesResult = result;

        if(result.data){

            console.log('Data:');
            console.log(result.data);
            this.opportunities = result.data;

            var i;
            for(i=0; i< result.data.length; i++) {
                console.log('Id: ', result.data[i].Id)
                opportunityRecords.push(result.data[i].Id);
                count ++;
            }
            
            console.log('opportunityRecords: ', opportunityRecords);
            console.log('Count: ', count);
            this.title = count + " Funded Opportunities";
            console.log('Title: ', this.title);

            this.activeSections = opportunityRecords;

        } else if(result.error){

            console.log('Error:');
            this.error = result.error;

        }
    }

    // Open Opp Subtab
    navigateToRecord(event) {
        console.log('Clicked! - Open Opp');
        console.log(event.target.dataset.id);
        this[NavigationMixin.Navigate]({
            type: 'standard__recordPage',
            attributes: {
                recordId: event.target.dataset.id,
                objectApiName: 'Opportunity',
                actionName: 'view'
            },
        });
    }

}
public class GetOpportunities {
    
    @AuraEnabled(cacheable=true)
    public static List<Opportunity> openOpportunities(string recordId) {
        String record = recordId;
        return [
            SELECT Id, Name, StageName, Amount, Type, Pipeline_Amount__c
            FROM Opportunity
            WHERE AccountId =:record AND IsClosed = FALSE
        ];
    }
    
    @AuraEnabled(cacheable=true)
    public static List<Opportunity> wonOpportunities(string recordId) {
        String record = recordId;
        return [
            SELECT Id, Name, Amount, CloseDate, Type, Funded_Amount__c, Fund_Date__c, Principal_Paid_Percentage__c, Lender_Account__r.Name  
            // Funded_Date__c
            FROM Opportunity
            WHERE AccountId = :record AND StageName = 'Funded'
            ORDER BY Fund_Date__c DESC
        ];
    }
    
    @AuraEnabled(cacheable=true)
    public static List<Opportunity> lostOpportunities(string recordId) {
        String record = recordId;
        return [
            SELECT Id, Name, Amount, CloseDate, Type
            // Application_Submitted_Date__c 
            FROM Opportunity
            WHERE AccountId = :record AND StageName != 'Funded' AND IsClosed = TRUE
        ];
    }
}

If anyone has any insight, it would be greatly appreciated. I am not totally sure as to why in some cases it works and in others it does not. It cannot be object/field permissions because I am admin & because it works at times... any help would be appreciated.

SOLUTION:


Based on the feedback and solution below here is what I did to resolve the error:

    @wire(getOppList, {recordId: '$recordId'})
    opportunitiesResult(result){

        console.log('Get Funded Opportunities Result:');
        console.log(result);
        console.log(result.data);

        var opportunityRecords = [];
        var count = 0;

        this.wiredOpportunitiesResult = result;

        if(result.data){

            console.log('Data:');
            console.log(result.data);

            var i;
            for(i=0; i< result.data.length; i++) {
                opportunityRecords.push(result.data[i].Id);
                count ++;

                let lenderAccount;

                if(result.data[i].Lender_Account__c == null){
                    lenderAccount = ""
                } else {
                    lenderAccount = result.data[i].Lender_Account__r.Name
                }

                let tempIt = {
                    Id: result.data[i].Id,
                    Name: result.data[i].Name,
                    Type: result.data[i].Type,
                    LenderAccount: lenderAccount,
                    FundedAmount: result.data[i].Funded_Amount__c,
                    FundedDate: result.data[i].Fund_Date__c
                }

                this.opportunities.push(tempIt);
            }
            
            console.log('opportunityRecords: ', opportunityRecords);
            console.log('Count: ', count);
            this.title = count + " Funded Opportunities";
            console.log('Title: ', this.title);

            this.activeSections = opportunityRecords;

        } else if(result.error){

            console.log('Error:');
            this.error = result.error;

        }
    }
4

If you don't have a Lender Account parent record, then, this error will occur.

You can either do a null check in the code or make a calculation field to resolve the name in the database.

In the Javascript controller you can extract the name into a variable:

let lenderAccountName = opportunity.Lender_Account__c ? opportunity.Lender_Account__r.Name : "";

Then just refer to that in the html file:

<dd class="slds-dl_horizontal__detail">
  {lenderAccountName}
</dd>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.