+ Start a Discussion
Big EarsBig Ears 

Lightning Web Components - How can we populate fields on a lightning-record-form

I'm creating a new Lightning Record Page layout for the standard Contact SObject. As part of this, we'd like to include the detail page for a related child record (it has a one-to-one relationship with the current record). We can't use a standard "Related Record" lightning component, as those only apply to parent records. We could make the related record a parent record (in theory), but I'd like to explore our options if we kept it as a child record.

I have created a Lightning Web Component which uses a lightning-record-form to pull in the details of the child record which is related to the current record. This is handled using a pretty simple set-up:

Template
<template>
    <lightning-record-form 
         record-id={childRecord.data} 
         object-api-name="childRecord__c" 
         layout-type="Full" 
         columns="1" 
         mode="view"> </lightning-record-form>
</template>

Javascript
import { LightningElement, wire, api, track } from 'lwc';
import getChildRecord from '@salesforce/apex/Contact_getChildRecordController.getChildRecord'; 

export default class Contact_CRSProfileLayout extends LightningElement { 
    @api recordId; 
    @track childRecordId; 

    @wire(getChildRecord, {contactId : '$recordId'})
    childRecordId; 
}

Apex Class
This is just a simple class which accepts the ContactId and returns the appropriate child record ID - Just a single line SOQL, effectively.

This works just fine, if the child record is already in the system.

However, if there isn't yet a child record in the system, I want the form to be displayed with the lookup to the current contact already populated. What I need to be able to do is access the lookup field and populate it with the current context record Id.

With aura components, this was possible, but I'm not sure how it would be achieved now? Is the record created in memory by lightning-record-form accessible to the javascript controller? If so, how?