function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Ertyq MrskErtyq Mrsk 

Name is Undefined When Trying to Display Custom Object's Field Values in Salesforce LWC

To get values of both CreatedById and CreatedDate, I used getFieldDisplayValue in my javascript file. After getting both values, I concatenate both values and displayed it in html. But when I check the value, only the CreatedDate displays the expected output, while CreatedById returns a null value. I would like to ask for your help regarding this issue.

transactionsLWC.html
<template>
    <div class="slds-theme_default">  
        <lightning-record-edit-form object-api-name="Transaction__c" record-id={recordId} layout-type="Full" mode="view">
            <lightning-messages></lightning-messages>
            <div class="slds-var-m-around_medium">
                <div class="slds-grid slds-wrap"> 
                    <div if:true={fscalctool.data}>   
                        <div class="slds-col slds-size_1-of-2">  
                            <label class = "slds-var-m-right_small slds-var-m-bottom_medium">Transaction Detail</label>      
                            <p>{createdByAndDateTime}</p>
                        </div>
                    </div> 
                </div>   
            </div>  
        </lightning-record-edit-form>
    </div>
</template>
transactionsLWC.js
import { LightningElement, api, wire } from 'lwc';
import { getRecord, getFieldDisplayValue } from 'lightning/uiRecordApi';
import CREATEDBYID_FIELD from '@salesforce/schema/Transaction__c.CreatedById';
import CREATEDDATE_FIELD from '@salesforce/schema/Transaction__c.CreatedDate';

const fields = [CREATEDBYID_FIELD, CREATEDDATE_FIELD];

export default class TransactionsLWC extends LightningElement {
    @api recordId;
    @api objectname;

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

    get createdById() {
        return getFieldDisplayValue(this.transaction.data, CREATEDBYID_FIELD);
    }

    get createdDate() {
        return getFieldDisplayValue(this.transaction.data, CREATEDDATE_FIELD);
    }

    get createdByAndDateTime() {
        return `${this.createdById},${this.createdDate}`;
    }
}
My expected value does not match the actual value.

Following is an example:

Expected: retailuser,10/19/2020,5:39 AM

Actual: null,10/19/2020,5:39 AM

 
Saravana Bharathi 1Saravana Bharathi 1
Hi Ertyq,

Instead of using getter function, you can make it synchronous, once the wired function executed with actual result, so that the data always flows to the variables.

Please refer below code:

@wire (getRecord,{ recordId: '$recordId', fields: [CREATEDBYID_FIELD, CREATEDDATE_FIELD] })
wiredRecord({ error, data }) {
if (error) {
let message = 'Unknown error';
} else if (data) {
this.createdByAndDateTime=getFieldValue(data, CREATEDDATE_FIELD)+''+getFieldValue(data, CREATEDBYID_FIELD);
}
}

If it solves the problem, please mark it as resolved.

Thanks