2

I'm trying to set the record-type-id of a <lightning-record-edit-form> in a Lightning Web Component. However, every time I add the code for the record type id, the page is blank. This is to create a new record, not edit one.

Controller:

import { getObjectInfo } from 'lightning/uiObjectInfoApi';
import MY_OBJECT from '@salesforce/schema/myObject__c';

export default class myClass extends LightningElement {

    @api recordTypeId;
    @api objectInfo;

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

    get recordTypeId() {
        console.log('get recordtypeid');
        // Returns a map of record type Ids 
        const rtis = this.objectInfo.data.recordTypeInfos;
        return Object.keys(rtis).find(rti => rtis[rti].name === 'record type 1');
    }
}

Component:

    <lightning-record-edit-form 
            object-api-name={myObject}
            record-type-id={recordTypeId}
            onsubmit={handleSubmit}
            onsuccess={handleSuccess}
            onerror={handleError}>
  • My guess is that the form is trying to load before the wire method is being called. You should probably try to move the form inside a <template if:true={objectInfo}> condition. – Martin Lezer Oct 8 '19 at 6:57
0

There's no tracking of your recordTypeId and no dependency linkage between getting the object info back from the server and this LWC property usage in the template. Try updating the code like:

@track
recordTypeId;

@wire(getObjectInfo, { objectApiName: MY_OBJECT })
handleObjectInfo({error, data}) {
    if (data) {
        const rtis = data.recordTypeInfos;
        this.recordTypeId = Object.keys(rtis).find(rti => rtis[rti].name === 'record type 1');
    }
}

That way when the recordTypeId changes the form markup in the template is re-processed. And the record type ID is only updated when the object info wire is actually invoked to successfully return data.

| improve this answer | |
  • Sorry, I didn't copy paste the code well. I updated the question to reflect what I have. – Vincent Oct 8 '19 at 9:43
  • 1
    Did you try making the getObjectInfo wire a function like I have suggested? You need to have this dependency linkage before setting the record type ID. – Phil W Oct 8 '19 at 9:47
  • I couldn't get this to work, so I'm just going to make my own form. Such a fundamental feature of SF should be easy to implement. Oh well. – Shane Jun 4 at 17:41
  • The lightning-record-form and lightning-record-edit-form components are a bit rough around the edges (e.g. Time fields are not handled well, you can't populate missing mandatory fields with defaults during submit, you can't pre-populate certain fields as the (record creation) form is initialized etc.). Still, writing your own from scratch is non-trivial... good luck! – Phil W Jun 4 at 19:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.