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
Antoine Fauque 10Antoine Fauque 10 

Get RecordTypeId in Lightning Web Component

Hi guys, i'm trying to get RecordTypeId in a Lightning Web Component which is located on the Account record page. 
I'll explain this component accesses a picklist field that is different depending on the record type. So I wanted to add record-type-id to the lightning-record-edit-form that is present on my component but in don't know how to get this id in my JS.
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" 

I'm not sure what to do to make it work.
If someone can help me on this or give me other way to get this recordTypeId
 
Best Answer chosen by Antoine Fauque 10
Ugur KayaUgur Kaya
Try this
 
@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);
        }
    };

 

All Answers

Ugur KayaUgur Kaya
Try this
 
@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);
        }
    };

 
This was selected as the best answer
Antoine Fauque 10Antoine Fauque 10
Thanks, I was not far from the solution