1

I'm trying to understand why there's a discrepancy in how you access data from a wired property depending on whether the data is a single record, or a collection (i.e returned by an apex method).

Single Record

When working with a single record, the template can display by data by using a getter method or using the long data.fields.name... syntax directly, see below where I'm querying two fields

 @wire(getRecord, { recordId: '$recordId', fields: [ accountName,'Account.Industry' ] } )
account;

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

Then, in the template, I can access them with a getter or directly

<template if:true={account.data}>
     {name} - {account.data.fields.Industry.value} 
 </template>

Collection

However, when working with a returned collection, it seems we can access the fields directly by iterating over the object.data property, for example

export default class ApexWireMethodToProperty extends LightningElement {
     @wire(getContactList) contacts;
 }

Then the template can access the data as follows

<template if:true={contacts.data}>
     <template for:each={contacts.data} for:item="contact">
         <p key={contact.Id}>{contact.Name}</p>
      </template>
 </template>

Notice how the template doesn't need to use the long syntax of

contact.data.fields.Name.value

If I try to use this shorter syntax in a single record component, it doesn't work. For example if we redo the first example

<template if:true={account.data}>
    {account.data.Industry} <<<< Does not work!
 </template>

Why?

3

In short, the first example is the Salesforce provided getRecord and the second is your own apex method. Salesforce's getRecord returns a different structure for the response than your custom method that returns a Contact list.


To elaborate, we need to understand what getRecord is returning. At the bottom of Get Record Data, it explains what it is designed to return:

The structure of the returned data is the same as the structure returned by the User Interface API that getRecord is built on. In this case, the response is Record.

If you take a look at the structure for "Record", you'll see that it returns that fields property that stores the field data for the record and explains why you need to access it the way you demonstrated contact.data.fields.Name.value.

enter image description here

You'll notice in the examples in the docs, they showcase a getFieldValues(this.account.data, REVENUE_FIELD) method to simplify getting the data as well. This is provided by Salesforce so we play with the data/structures they give us.

In your custom apex, you're returning a list of contact records. This is why you can iterate (for:each) through each Contact record in the list and access the data directly. Just like you would do in apex. There's no encompassing "fields" property that is encapsulating the field data for this record.

| improve this answer | |

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.