You need to sign in to do that
Don't have an account?
JeffreyStevens
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...
The APEX is:
The HTML is:
But the LWC doesn't display. If I change the HTML to
Can anybody see what I'm doing wrong?
Thanks,
(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)
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?
Thanks,
(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)
What is your wired method? There are two forms
import rtnSObjectRec from '@salesforce/apex/HelperAccount.rtnSObjectRec';
@wire(rtnSObjectRec , { recId: '$recId' })
acc;
https://developer.salesforce.com/docs/component-library/documentation/lwc/lwc.apex
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: https://developer.salesforce.com/docs/component-library/documentation/lwc/lwc.data_load_record
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>