+ Start a Discussion

LWC fields from oObject html syntax

I have a LWC that is returning an sObject from APEX.  How do you display a field in the HTML?

my .js has...
import rtnSObjectRec from '@salesforce/apex/HelperAccount.rtnSObjectRec';

The APEX is:
@AuraEnabled (cacheable=true) 
	public static Account rtnSObjectRec(string recId) {
		Account sObjectRec = [SELECT id,name FROM Account WHERE Id = :recId];
		return sObjectRec;

The HTML is:
<lightning-formatted-text value={sObjectRec.name.data}></lightning-formatted-text>

But the LWC doesn't display.  If I change the HTML to
<lightning-formatted-text value="testData"></lightning-formatted-text>
The lwc will display, and the "testData" will show.

Can anybody see what I'm doing wrong?


(I know I can just use the data service to get sObjects, I'm just trying to understand the syntax on the HTML side. Eventually, I want my APEX to return a custom sub-class)

Alain CabonAlain Cabon

 What is your wired method?   There are two forms

import rtnSObjectRec from '@salesforce/apex/HelperAccount.rtnSObjectRec';
@wire(rtnSObjectRec , { recId: '$recId' })


But the display in the template of a field from acc is not direct with just acc.Name. LWC is more verbose.

import NAME_FIELD from '@salesforce/schema/Account.Name';   // one declaration by fied? .... yes, it is verbose.
getFieldValue(this.acc.data, NAME_FIELD)

Complete example:
        <div class="slds-grid">
            <div class="slds-col slds-size_1-of-2">
                <!-- Other record data here -->
            <div class="slds-col slds-size_1-of-2">
                <lightning-formatted-text value={nameValue}
import { LightningElement, api, wire } from 'lwc';
/* Wire adapter to fetch record data */
import { getRecord, getFieldValue } from 'lightning/uiRecordApi';
import ACCOUNT_OBJECT from '@salesforce/schema/Account';
import NAME_FIELD from '@salesforce/schema/Account.Name';

export default class AccountViewer extends LightningElement {
     /** Id of record to display. */
    @api recordId;

    /* Expose schema objects/fields to the template. */
    accountObject = ACCOUNT_OBJECT;

    /* Load Account.Name for custom rendering */
    @wire(getRecord, { recordId: '$recordId', fields: [NAME_FIELD] })

    /** Get the Account.Name value. */
    get nameValue() {
        return this.record.data ? getFieldValue(this.record.data, NAME_FIELD) : '';

There are utility methods getRecord / getFieldValue and finally a getter for just getting the name value of the account.

<lightning-formatted-text value={nameValue} class="slds-text-heading_large"> </lightning-formatted-text>