3

I'm trying to create record Type selection screen in LWC, but getting this error

afterRender threw an error in 'c:createContact' [Cannot read property 'recordTypeInfos' of undefined]

HTML:

<template>
    <lightning-combobox name="recordType" label="Record Type" placeholder="Choose Status"
                        value={value} 
                        options={recordTypeId}>

    </lightning-combobox>
</template>

JS:

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

export default class CreateContact extends LightningElement {

    @track statusOptions;
    @track value;
    @api objectApiName;

    @track objectInfo;

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


    get recordTypeId() {
        // Returns a map of record type Ids 
        console.log(this.objectInfo);
        const rtis = this.objectInfo.data.recordTypeInfos;
        return rtis;
    }

}
1
  • Use console.log(JSON.stringify(this.objectInfo)); to see what data you have.
    – Keith C
    Commented May 19, 2019 at 12:58

1 Answer 1

12

First thing i would do is put the combobox inside a div and only open it up once the wire data for recordtype comes from the object.

<div if:true={objectInfo.data}>
<lightning-combobox name="recordType" label="Record Type" placeholder="Choose Status"
value={value} 
options={recordTypeId1}>
</lightning-combobox>
</div>

Secondly in javascript where you are constructing the combobox for UI ,you need to match the format of lightning combobox specified in its documentation

 get recordTypeId1() {
      var recordtypeinfo = this.objectInfo.data.recordTypeInfos;
      var uiCombobox = [];
      console.log("recordtype" + recordtypeinfo);
      for(var eachRecordtype in  recordtypeinfo)//this is to match structure of lightning combo box
      {
        if(recordtypeinfo.hasOwnProperty(eachRecordtype))
        uiCombobox.push({ label: recordtypeinfo[eachRecordtype].name, value: recordtypeinfo[eachRecordtype].name })
      }
      console.log('uiCombobox' + JSON.stringify(uiCombobox));
    return uiCombobox;
  }

Once the wire data comes back the combobox will openup in markup and your getter function will run

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

Also the default value for the combo box UI, put a value which is in your object.

@track value = 'Client User';
5
  • Its working, but if I remove the if condition from the div then why I'm getting "afterRender threw an error in 'c:createContact' [Cannot read property 'recordTypeInfos' of undefined]" error? Commented May 24, 2019 at 10:17
  • 1
    @salesforceDeveloper - its a race condition, the wire data has not come back and you are trying to access objectInfo data through the getter expression function recordTypeId, thats why you need to open up the getter once the data comes back
    – RedDevil
    Commented May 24, 2019 at 16:30
  • Thanks for answer Commented May 25, 2019 at 18:03
  • 2
    Consider replacing the <div> with <template> to avoid influencing the rendered DOM structure Commented Jul 17, 2019 at 15:28
  • FYI if you like concise syntax you can use Object.keys(recordtypeinfo).map(id => { value: id, label: recordtypeinfo[id].name }). The cautious might wish to .filter(id => { return recordtypeinfo[id].available }) before calling .map.
    – Adrian Larson
    Commented May 21, 2020 at 0:29

You must log in to answer this question.

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