Please replace from "import { getObjectInfo } from '@salesforce/schema/';" to "import { getObjectInfo } from 'lightning/uiObjectInfoApi';" from line no. 2.
I have updated that code. and please check the following working solution to retrieve dynamic object fields.
DynamicSObjectLWC.js
import { LightningElement, api, wire } from 'lwc';
import { getObjectInfo } from 'lightning/uiObjectInfoApi';
import { refreshApex } from '@salesforce/apex';
export default class DynamicSObjectLWC extends LightningElement {
@api sObjectName;
@wire(getObjectInfo, { objectApiName: '$sObjectName' })
objectInfo;
handleChange(event) {
this.sObjectName = this.template.querySelector('[data-name="objectName"]').value;
refreshApex(this.objectInfo).then(() => {
// do something with the refreshed data in this.objectInfo
console.log(this.fields);
});
}
get fields() {
if (this.objectInfo.data) {
return this.objectInfo.data.fields;
}
return [];
}
}
DynamicSObjectLWC.html
<template>
<lightning-card>
<lightning-layout>
<lightning-layout-item>
<lightning-input data-name="objectName" label="Enter object name"></lightning-input>
<lightning-button label="Click" onclick={handleChange}></lightning-button>
</lightning-layout-item>
</lightning-layout>
</lightning-card>
</template>
Please review this example
6 answers