2

I am trying to get all the record type options for Opportunity. I am using getObjectInfo directly copied from the documentation but I keep getting the same error:

Documentation: https://developer.salesforce.com/docs/component-library/documentation/en/lwc/lwc.reference_wire_adapters_object_info

JS:

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


export default class New_Opportunity extends LightningElement {

    @api recordId;
    @track objectInfo;

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

    get recordTypeOptions() {
        console.log('options ', this.objectInfo.data.recordTypeInfos);
    }

    connectedCallback() {
        console.log('recordId: ', this.recordId);
        console.log(JSON.stringify(this.objectInfo));
    }
}

enter image description here

I am really unsure why this is not working as I have directly copied the code

1 Answer 1

2

The example is wrong in several places. First, the variable is declared twice, which is a bad practice. Second, the data property will be undefined the first time your getter is called. Third, connectedCallback will be called before the wire completes, so this.objectInfo would not have any data in it. Fourth, the name of the component would cause the file to be named new_Opportunity and accessed as c-new_-opportunity, which would be annoying and not "normal" compared to how names should look. Fifth, the getter doesn't return a value. This makes it useless and technically incorrect.

It should look like this:

import { LightningElement, wire, api } from 'lwc';
import { getObjectInfo } from 'lightning/uiObjectInfoApi';
import OPPORTUNITY_OBJECT from '@salesforce/schema/Opportunity';


export default class NewOpportunity extends LightningElement {

    @api recordId;

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

    get recordTypeOptions() {
      if(this.objectInfo.data) {
        console.log('recordTypeInfos', 
          JSON.stringify(
            this.objectInfo.data.recordTypeInfos
          )
        );
        return this.objectInfo.data.recordTypeInfos;
      } else {
        return {}; // An empty mapping for now...
      }
    }
    connectedCallback() {
        console.log('recordId: ', this.recordId);
    }
}
3
  • P.S. I've sent a message to the Salesforce Documentation to have this corrected.
    – sfdcfox
    May 20, 2022 at 15:23
  • Thank you very much! @sfdcfox
    – some guy
    May 20, 2022 at 15:28
  • @someguy You're welcome!
    – sfdcfox
    May 20, 2022 at 15:33

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .