Salesforce Interview Questions & Answers Book is now available!

Grab Now

LWC

How to get the ObjectInfo in LWC

How to get info about the object using getObjectInfo in LWC.

By Teja, ex-Salesforce engineer · Updated April 6, 2021 · 1 min read

When we are working with Lightning Web Components, we might comes across a business requirement where need some info about the object.

When I say Info am referring to metadata describing the object’s fields, child relationships, record type, and theme.

Don't try to invoke an Apex method use Describe class and get the required info. Make use of out of the box available feature.

In that case, we can make use of one of the named imports that we get access to out of the box and that is getObjectInfo

Let me show you a snippet using getObjectInfo

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 })
    accountObject({error, data}){
        if(data){
            console.log(data);
        }else{
            console.log(error);
        }
    }
}

Hope this is helpful!

Learn it by building it

Every course, every code repository, the discussion group and certificates of completion — one subscription.

Keep me in the loop!

Email me when a new episode is published (promise, I will not spam you)

You're signing up to receive emails from Salesforce Casts