0

I work with the Lead object in an LWC component and I am new to LWC. I Can't get to do what I want with the lightning-record-*-form components so I intend to sur the @wire data link. TO better discover the use of @wire, I use and execute the "Get Record Data" sample code given in the documentation: enter link description here

The example works fine. So I decide to replace the Contact object of the example with a Lead object, and it fails with the following message: "Cannot read properties of undefined (reading 'data')". Lines and Columns are undefined.

So I revert to the version with Contact, and all works fine again.

Hence my question: is there an issue with the Lead object with LWC?

Notice: if I use the original code and pass it a Lead record ID, it works ok. Error is raised when I use the word 'Lead' or 'lead' in the LWC javaScript code.

Thank you for your help.

THe LWC code below:

HTML:

<template>
<lightning-card title="My Contact Record" icon-name="standard:contact">
    <template if:true={contact.data}>
        <div class="slds-m-around_medium">
            <p>{name}</p>
            <p>{title}</p>
            <p><lightning-formatted-phone value={phone}></lightning-formatted-phone></p>
            <p><lightning-formatted-email value={email}></lightning-formatted-email></p>
        </div>
    </template>
    <template if:true={contact.error}>

    </template>
</lightning-card>

JavaScript:

import { LightningElement, api, wire } from 'lwc';
import { getRecord } from 'lightning/uiRecordApi';

const FIELDS = [ 'Lead.Name', 'Lead.Title', 'Lead.Phone', 'Lead.Email', ];

export default class WireGetRecordDynamicContact extends LightningElement {
   @api recordId;

   @wire(getRecord, { recordId: '$recordId', fields: FIELDS })
   lead;

   get name() {
       return this.lead.data.fields.Name.value;
   }

   get title() {
       return this.lead.data.fields.Title.value;
   }

   get phone() {
       return this.lead.data.fields.Phone.value;
   }

   get email() {
       return this.lead.data.fields.Email.value;
   }
}

2 Answers 2

1

If the record is not in the LDS (Lightning Data Service) cache, the value for data is null until after a render cycle. This is likely why it's working for some records, and not others. You should always use getFieldValue to avoid this error. Also, consider using field tokens, as described in the documentation.

import { LightningElement, api, wire } from 'lwc';
import { getRecord, getFieldValue } from 'lightning/uiRecordApi';
import LEAD_NAME from '@salesforce/schema/Lead.Name';
import LEAD_TITLE from '@salesforce/schema/Lead.Title';
import LEAD_PHONE from '@salesforce/schema/Lead.Phone';
import LEAD_EMAIL from '@salesforce/schema/Lead.Email';


const FIELDS = [ LEAD_NAME, LEAD_PHONE, LEAD_EMAIL, LEAD_TITLE ];

export default class WireGetRecordDynamicContact extends LightningElement {
   @api recordId;

   @wire(getRecord, { recordId: '$recordId', fields: FIELDS })
   lead;

  get name() {
    return getFieldValue(this.lead.data, LEAD_NAME);
  }

  get title() {
    return getFieldValue(this.lead.data, LEAD_TITLE);
  }

  get phone() {
    return getFieldValue(this.lead.data, LEAD_PHONE);
  }

  get email() {
    return getFieldValue(this.lead.data, LEAD_EMAIL);
  }
}
1
  • Thanks for the advice and explanations, but sitll doesn't work and returns the same behavior and error. FYI: the code execution doesn't get to the 'rendredCallback()' stage. What is really strange is: when I change Lead to Contact in the code, it works pefrectly as expected, event if when recordId has a Lead ID. It really looks linked to the usage of the Lead word or object.
    – fredbe
    Commented Feb 8, 2022 at 8:21
0

No problem at the end : the error raised was linked to the HTML page which uses a variable that don't exist anymore when I change Contact by Lead. The ony problem is some kind fatigue and consequently a slow brain. Sorry.

You must log in to answer this question.

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