Monday, 17 April 2023

How to get record type name from recordId in Lighting Component without Apex ?

 First, the code imports the getRecord wire adapter from the lightning/uiRecordApi module, which is built on Lightning Data Service. Then it defines the fields (Recordtypeid,recordtypename])to pass to the wire adapter.

The @wire decorator tells getRecord to get the values of the specified fields on the record with the specified $recordId. The $ means that the value is passed dynamically. When the value changes, the wire service provisions data and the component rerenders.

The data is provisioned to the data and error objects of the property decorated with @wire.

import { LightningElement, api, wire } from 'lwc';

import { getRecord } from 'lightning/uiRecordApi';
import CASE_RECORDTYPEID from '@salesforce/schema/Case.RecordType.Id'
import CASE_RECORDTYPENAME from '@salesforce/schema/Case.RecordType.Name'
const _FIELDS = [CASE_RECORDTYPEID,CASE_RECORDTYPENAME];
export default class PlanOfCareTab extends LightningElement {
    @api recordId;
    caseRec;
    recordtypeName;

   
    @wire(getRecord, { recordId: '$recordId', fields: _FIELDS })
    wiredRecord ({data, error} ) {
        console.log(' data----34--', JSON.stringify(data));
        if(data){
            this.caseRec = data;
            console.log(' this.caseRec--', JSON.stringify(this.caseRec));
            this.recordtypeName =
JSON.stringify(this.caseRec.fields.RecordType.value.fields.Name.value);
            this.recordtypeName = this.recordtypeName.replace(/"/gi, ""); // replaces ""
   
        }
        if(error){
            console.log('error occured');
        }
    }

}

No comments: