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
Phuc Nguyen 18Phuc Nguyen 18 

LWC able to retrieve record Id but need to get other record fields

Hello All,
I am working on a LWC.  Currently I can retrieve the record Id and pass it to a handler
@api recordId;

console.log('Job RecordId is ' + this.recordId);

getContactName{recordId : this.recordId})

Now I want to pass another field off the record but unsure how to retrieve it.  I tried this but it does not work
import JOB_NAME_FIELD from '@salesforce/schema/Contact__c.Job_Name__c';

getFieldValue(this.recordId, JOB_NAME_FIELD);

Any suggestions would be greatly appreciated.
Thanks
P
 
Best Answer chosen by Phuc Nguyen 18
Krishna VKrishna V
Phuc Nguyen, 

Please try the below and see if it works- 
 
/// We will first want to import get record from Lightning/UiRecordApi.
import {
    getRecord
} from 'lightning/uiRecordApi';
// then we can name as many fields as we want from the object in the below fashion. 
import NAME_FIELD from '@salesforce/schema/User.Name';
import EMAIL_FIELD from '@salesforce/schema/User.Email';

// Once above is implemented, we can use the wire service adapter to fetch the fields. 

@wire(getRecord, {
        recordId: Id,
// As you can see here, instead of the field itself, like you have implemented, we are using 'fields' attribute from getrecord method to feed all the field values we need.
        fields: [NAME_FIELD, EMAIL_FIELD]
    }) wireuser({
        error,
        data
    }) {
        if (error) {
            this.error = error;
        } else if (data) {
            this.userName = data.fields.Name.value;
            this.userEmail = data.fields.Email.value;
            this.userPidm = data.fields.PIDM__c.value
        }
    }

Note: for this to work, make sure you import 'wire' decorator to your component along with api.

Let me know if this worked for you. 
 

All Answers

Krishna VKrishna V
Phuc Nguyen, 

Please try the below and see if it works- 
 
/// We will first want to import get record from Lightning/UiRecordApi.
import {
    getRecord
} from 'lightning/uiRecordApi';
// then we can name as many fields as we want from the object in the below fashion. 
import NAME_FIELD from '@salesforce/schema/User.Name';
import EMAIL_FIELD from '@salesforce/schema/User.Email';

// Once above is implemented, we can use the wire service adapter to fetch the fields. 

@wire(getRecord, {
        recordId: Id,
// As you can see here, instead of the field itself, like you have implemented, we are using 'fields' attribute from getrecord method to feed all the field values we need.
        fields: [NAME_FIELD, EMAIL_FIELD]
    }) wireuser({
        error,
        data
    }) {
        if (error) {
            this.error = error;
        } else if (data) {
            this.userName = data.fields.Name.value;
            this.userEmail = data.fields.Email.value;
            this.userPidm = data.fields.PIDM__c.value
        }
    }

Note: for this to work, make sure you import 'wire' decorator to your component along with api.

Let me know if this worked for you. 
 
This was selected as the best answer
Phuc Nguyen 18Phuc Nguyen 18
Hello KRISHNA,
Thank you for the reply and suggestion.  Can I put this inside a connectedcallback?
Thank you,
P
Krishna VKrishna V
You do not have to put this inside a lifecycle event hook. Since wire is a reactive service, the variable will be updated whenever the record Id changes, in this case, when the cmp is rendered. You will have the values available to you inside the connectedCallBack if your design requires you to.