function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Phil WPhil W 

How can I pre-populate a master detail field in lighting-record-form?

I am writing a custom LWC, one aspect of which is the ability to create a new record based on the context of that custom LWC instance. The LWC component holds the ID of a parent object. The child has a Master Detail relationship back to the parent.

When my component creates the new record, I want to use lightning-record-form. The reason is that I want the form to include all fields that the admin has populated into the full layout - my LWC component is part of a package and I cannot make any assumption about the fields that should or should not be in the form over and above the master details field. Use of the lightning-record-edit-form therefore doesn't make sense (since I have to explicitly define the list of fields to be shown).

I am trying to work out how to use the parent ID from the LWC component to pre-populate the Master Details field in the new record, but all my research leads me to the answer that I cannot.

There is an event, "load", generated by the lightning-record-form component, and this provides access to the record data. However, according to what I've read on Stack Exchange, setting the field values in here does NOT cause the form to update its Master Detail field.

There is another event, "submit", generated when the user tries to save the new record, and according to the documentation (https://developer.salesforce.com/docs/component-library/bundle/lightning-record-form/documentation, in the "Editing a Record" section) this event can intercept the data to explicitly override values for certain fields. However, since I'm trying to set what is effectively a mandatory field, I suspect my submit handler won't be invoked before the form decides it is missing a mandatory value.

Has anyone actually got this working? How did you do it?