0

I'm new to development, and using an LWC component with an Apex class for the first time. I have written an Apex Class that seems to function as expected when tested, but I'm running in to issues getting any info back from it in the LWC. I think that my issue is that I'm not actually getting the recordId of the current record, but so far nothing that I've tried has fixed it.

What am I doing wrong?

Expected Outcome:

proofsData should return the output of the Apex class method 'fetchProofRecordList'. This output should be a List of Proof__c records and their values. I then would like to render those field values within the LWC HTML in a manner similar to a related records list.

LWC js:

import { LightningElement,api,wire} from 'lwc';
import fetchProofRecordList from '@salesforce/apex/ProofRelatedListController.fetchProofRecordList';


export default class NearbyProofsWidget extends LightningElement {

    @api recordId;

    @wire (fetchProofRecordList,{recordId:'$recordId'})
    proofsData;

}

Apex Class:

public class ProofRelatedListController {
    
    @AuraEnabled(cacheable=true)
    public static List<Proof__c> fetchProofRecordList(String recordId){
        Account AcctGeoLocation = [Select BillingLatitude,BillingLongitude FROM Account WHERE Id = :recordId];
        
        Double AcctLat = AcctGeoLocation.BillingLatitude;
        Double AcctLon = AcctGeoLocation.BillingLongitude;
        
        Proof__c[] recordList = [SELECT Id, Title__c, Product__c, Feature__c 
                                 FROM Proof__c 
                                 WHERE DISTANCE(Customer_Account__r.BillingAddress,GEOLOCATION(:AcctLat,:AcctLon),'mi') < 50
                                 LIMIT 5
                                ];
        
        system.debug('Results are:'+ recordList);
        system.debug(recordList.size() + 'proofs found');
        Return recordList;
    }

}

Edited to add XML and Screenshot

<?xml version="1.0" encoding="UTF-8"?>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">
    <apiVersion>52.0</apiVersion>
    <isExposed>true</isExposed>
    <masterLabel>Proof Related List</masterLabel>
    <targets>
        <target>lightning__RecordPage</target>
    </targets>
</LightningComponentBundle>

LWC Error Screenshot

2
  • 1
    Hi, and welcome to SFSE! Please take a moment to read about How to Ask and take the tour. As written, aside from the variable name issue, there's no reason why this shouldn't work as written, so we must presume that the error lies in how you're using the LWC or how it's configured. What's your XML look like? Where is your LWC located? You can edit your question to include additional information to help us figure out what's going on.
    – sfdcfox
    Commented Sep 20, 2021 at 14:54
  • Thanks, I have added more info on the XML and the error message. I am using the LWC as a custom Related Records list, that relates based on distance, not Account lookup. It would be added as a lightning card to the Account Page Layout from the Lightning App Builder.
    – BlakeSFDC
    Commented Sep 20, 2021 at 15:48

3 Answers 3

1

For starters, the property name in the wired method, needs to match the one of your apex class property name that is passed:

@wire (fetchProofRecordList,{recordId:'$recordId'})

public static List<Proof__c> fetchProofRecordList(String recordIdInput){

recordId is not equal to recordIdInput

you should amend that.

Additionally to the recordId being undefined, it is likely that the wired method is fired several times, since you are binding the recordId value dynamically.

It might fire once, when the component renders, and fire additionally, when the recordId changes from 'undefined' to its actual value.

you can do 2 things to address this.

add a template directive and check the value using if:true

or use a wired function, instead of a property, as indicated in the docs:

wiredproofData({ error, data }) {
        if (data) {
            this.proofData= data;
            this.error = undefined;
        } else if (error) {
            this.error = error;
            // add any other handling
        }
    }
4
  • I forgot that I had tried to change that during one of my efforts to fix the issue. I reverted that back, but am still receiving the error 'proofsData is Undefined when trying to load the LWC.
    – BlakeSFDC
    Commented Sep 20, 2021 at 14:39
  • you will have to add further details to the post, more specifically, how you are assigning the recordId
    – glls
    Commented Sep 20, 2021 at 15:00
  • To retrieve the Id of the current record, I understood that I needed to use the decorator @api recordId; , is that correct?
    – BlakeSFDC
    Commented Sep 20, 2021 at 15:54
  • added additional feedback
    – glls
    Commented Sep 20, 2021 at 17:16
0

if you have bind this variable "proofsData" on HTML so Please try with null checking --> if:true={proofsData}

1
  • I've done that. Still getting the same error message that proofsData is Undefined.
    – BlakeSFDC
    Commented Sep 20, 2021 at 17:20
0

So, after some more Googling, I finally figured out what my issue was. It turns out it was actually an HTML issue, not anything with the Apex Class, or the Javascript.

The link to this Stackexchange article ended up pointing me in the right direction. Turns out I was using the wrong format for the iteration. Once I got that sorted, I was able to display my data.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .