getObjectInfo Wire Adapter

Let us discuss here how to use this wire adapter getObjectInfo wire adaptor to get metadata about a specific object. The response includes metadata describing fields, child relationships, record type, and theme. Here is the sample Syntax on how to invoke the getObjectInfo wire adapter.

Syntax 

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;
}

 

  • objectApiName—(Required) A supported object.
  • propertyOrFunction—A private property or function that receives the stream of data from the wire service. If a property is decorated with @wire, the results are returned to the property’s data property or error property. If a function is decorated with @wire, the results are returned in an object with a data property and an error property.

Create Lightning web component

create a lightning web component using the following SFDX command.

sfdx force:lightning:component:create --type lwc -n ObjectInfo -d force-app/main/default/lwc

Use this ObjectInfo.html markup code

<template>
    <lightning-card title="Wire Get Object Info" icon-name="custom:custom67">
    <div class="slds-m-horizontal_medium">
        <template if:true={objectInfo.data}>
                <div class="scroller">
                    <pre>{objectInfoStr}</pre>
                </div>
            </template>
        </div>
        <template if:true={objectInfo.error}>
            Error .Please COntact your Admin .
        </template>
    </lightning-card>
</template>

Use this ObjectInfo.js code

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

export default class ObjectInfo extends LightningElement {
    @api objectApiName;
    @track objectInfo;

    @wire(getObjectInfo, {
        objectApiName: '$objectApiName'
    })
    objectInfo;
    get objectInfoStr() {
        return this.objectInfo ?
            JSON.stringify(this.objectInfo.data, null, 2) :
            '';
    }
}

Use this ObjectInfo.js-meta.xml code

<?xml version="1.0" encoding="UTF-8"?>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata" fqn="helloWorld">
    <apiVersion>45.0</apiVersion>
    <isExposed>true</isExposed>
    <targets>
        <target>lightning__RecordPage</target>
    </targets>
</LightningComponentBundle>

Push changes to scratch org.

Push changes to scratch using this command and add this component to record page using lighting app builder

After adding the lightning web component to record page using the app builder. you can able to see the output as shown below

 

 

Understand the code

The following code returns the sobject data using the getObjectInfo wired adapter

 @api objectApiName;
    @track objectInfo;

    @wire(getObjectInfo, {
        objectApiName: '$objectApiName'
    })
    objectInfo;

 

The following markup will show the json data into UI

 <template if:true={objectInfo.data}>
                <div class="scroller">
                    <pre>{objectInfoStr}</pre>
                </div>
            </template>