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
Henry MichelHenry Michel 

How to get RecordTypeId in Lightning Web Component

I tried this : 
 
import { LightningElement, api, wire } from "lwc"; import { getRecord } from "lightning/uiRecordApi"; import ACCOUNT_RECORDTYPE_FIELD from '@salesforce/schema/Account.RecordTypeId'; export default class Account_Redirect extends NavigationMixin(LightningElement) { @api recordId; @api objectApiName; @wire(getRecord, { recordId: '$recordId', fields: [ACCOUNT_RECORDTYPE_FIELD] }) acc; get recordTypeId() { return this.acc.data.fields.RecordTypeId.value; } }
But i have the following error : 
"Cannot read property 'fields' of undefined" 
Best Answer chosen by Henry Michel
Deepali KulshresthaDeepali Kulshrestha
Hi Henry,

Greetings to you!

I have written the following code as per your requirement. Please try the below code.
@track account;
    @track recordTypeId;
    @wire(getRecord, { recordId: '$recordId', fields: [ACCOUNT_RECORDTYPE_FIELD] })
    getAccount({ error, data }){
    if(data){
        var result = JSON.parse(JSON.stringify(data));
        console.log('acc data: ', result);
        this.account = result;
        this.recordTypeId = result.fields.RecordTypeId.value;
    
        }else if(error) {
            var result = JSON.parse(JSON.stringify(error));
            console.log('error: ', result);
        }
    };

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha
www.kdeepali.com

All Answers

Khan AnasKhan Anas (Salesforce Developers) 
Hi Henry,

Greetings to you!

You can try like below:
@track account;
    @track recordTypeId;
    @wire(getRecord, { recordId: '$recordId', fields: [ACCOUNT_RECORDTYPE_FIELD] })
    getAccount({ error, data }){
    if(data){
        var result = JSON.parse(JSON.stringify(data));
        console.log('acc data: ', result);
        this.account = result;
        this.recordTypeId = result.fields.RecordTypeId.value;
    
        }else if(error) {
            var result = JSON.parse(JSON.stringify(error));
            console.log('error: ', result);
        }
    };

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas
Deepali KulshresthaDeepali Kulshrestha
Hi Henry,

Greetings to you!

I have written the following code as per your requirement. Please try the below code.
@track account;
    @track recordTypeId;
    @wire(getRecord, { recordId: '$recordId', fields: [ACCOUNT_RECORDTYPE_FIELD] })
    getAccount({ error, data }){
    if(data){
        var result = JSON.parse(JSON.stringify(data));
        console.log('acc data: ', result);
        this.account = result;
        this.recordTypeId = result.fields.RecordTypeId.value;
    
        }else if(error) {
            var result = JSON.parse(JSON.stringify(error));
            console.log('error: ', result);
        }
    };

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha
www.kdeepali.com
This was selected as the best answer
sivainkecsivainkec
I am getting the error "Invalid reference Opportunity.RecordType.ID of type sobjectField in file opportunityDetails.js" when am importing recordtypeid field like below.

import OPPORTUNITY_RECORDTYPE_FIELD from '@salesforce/schema/Opportunity.RecordType.ID';