Introducing getObjectInfo For Lightning Web Components!


Have you ever needed to retrieve metadata information about a specific object in Salesforce.

Use getObjectInfo a powerful wire adapter allows you to fetch metadata about an object, including its fields, child relationships, record type, and even the theme.

Syntax example:

import { LightningElement, wire } from "lwc";
import { getObjectInfo } from "lightning/uiObjectInfoApi";
import ACCOUNT_OBJECT from "@salesforce/schema/Account";

export default class Example extends LightningElement {
  @wire(getObjectInfo, { objectApiName: ACCOUNT_OBJECT })
  propertyOrFunction;
}

This handy resource leverages the User Interface API and supports both single object retrieval with getObjectInfo and batch retrieval with getObjectInfos.

Here is an example to retrieve the record type Ids and find the Id that matches the record type name "Special Account".

import { LightningElement, api, wire, track } from "lwc";
import { getObjectInfo } from "lightning/uiObjectInfoApi";
import ACCOUNT_OBJECT from "@salesforce/schema/Account";

export default class RecordFormWithRecordType extends LightningElement {
  // Flexipage provides recordId and objectApiName
  @api recordId;
  @api objectApiName;
  @track objectInfo;

  @wire(getObjectInfo, { objectApiName: ACCOUNT_OBJECT })
  getObjectInfo({ error, data }) {
    if (data) {
      this.objectInfo = data;
    } else if (error) {
      // Handle error
    }
  }

  get recordTypeId() {
    // Returns a map of record type Ids
    const rtis =
      this.objectInfo && this.objectInfo.data
        ? this.objectInfo.data.recordTypeInfos
        : null;
    return rtis
      ? Object.keys(rtis).find(
          (rti) => rtis[rti].name === "Special Account"): null;
  }
}

Follow Us

Posted By : Sudeer Kamat Date :

view_module Related

label Labels

Comments 0