Lightning – Fixing lightning:recordEditForm to Create Children on Master-Detail Relationship

<aura:component implements="force:appHostable">
<aura:attribute name="recordId" type="String" />
<lightning:card title="Create Reservation">
    <lightning:recordEditForm
                              onsuccess="{!c.handleSuccess}"
                              objectApiName="Campsite_Reservation__c">
        <!-- the messages component is for error messages -->
        <lightning:messages />
        <lightning:inputField fieldName="Campsite__c" value="a041o00000ZWoFbAAL"/> 
        <lightning:inputField fieldName="Reference__c"/>
        <div class="slds-m-top_medium">
            <lightning:button variant="brand" type="submit" name="save" label="Save" />
        </div>
    </lightning:recordEditForm>
</lightning:card>   

({

    handleSuccess: function(cmp, event, helper) {
       var params = event.getParams();
       cmp.set('v.recordId', params.response.id);
    }
})

enter image description here

I'm setting the Id of the master record as the value on the new child record which should get created. When the save button is clicked, an error message is displayed as per the screenshot. Is this a known limitation?

Best Answer

This is a weird issue even if in the logs the error message is not clear. The log just says REQUIRED_FIELD_MISSING.

{"message":"An error occurred while trying to update the record. Please try again.","stackTrace":"","data":{"message":"An error occurred while trying to update the record. Please try again.","statusCode":400,"enhancedErrorType":"RecordError","output":{"errors":[],"fieldErrors":{"Campsite__c":[{"constituentField":null,"duplicateRecordError":null,"errorCode":"REQUIRED_FIELD_MISSING","field":"Campsite__c","fieldLabel":"Campsite","message":"Required fields are missing: [Campsite__c]"}]}}},"id":"-829756038"}"

There is a workaround available to solve this issue. You have to set the Reparentable Master Detail to true for Campsite__c and it should work.