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
Bryan Leaman 6Bryan Leaman 6 

Can I use lightning:recordEditForm to create a related record?

I have a lightning component using force:hasRecordId.  What I've been trying to do (unsuccessfully) is have a subform that just needs one lookup field and automatically fill in the current parent object's record Id and write the new related record.

The problem is the fields don't even render. Hidden in the html is
<span class="slds-hide">
 "No input rendered: "
"Field "undefined" not found in response."
</span>

I've used lightning:recordeditform before, but only to create & update the object record who's page I'm on, not a related list record.

I'm getting the main record's data using force:recordData (this works fine):
<force:recordData aura:id="dealEditor"
        recordId="{!v.recordId}"
        fields="Id,Name"
        targetFields="{!v.dealFields}"
        targetError="{!v.dealError}"
        recordUpdated="{!c.dealHandleRecordUpdated}"
        mode="EDIT"
    />

On successful load of the force:recordData  I'm setting the new credit application's Proposal__c field so the new record will be properly linked: 
component.find("newca_proposal").set("v.value", component.get("v.dealFields.Id"));

And I want to allow lookup of one field and then write a related record:
<aura:attribute name="newCreditAppId" type="String"/>
                <lightning:recordEditForm aura:id="newcreditapp"
                    recordId="{!v.newCreditAppId}"
                    objectApiName="dealer__Credit_Application__c"
                    onsubmit="{!c.newCreditAppSave}"
                    class="slds-card__body_inner"
                >
                    <lightning:inputField fieldname="Finance_Company__c" aura:id="newca_financecompany"/>
                    <lightning:inputField fieldname="Proposal__c" aura:id="newca_proposal"/>
                    <lightning:button type="submit" label="Add Finance Company"/>
                </lightning:recordEditForm>

Is this possible? What am I missing?
Best Answer chosen by Bryan Leaman 6
Bryan Leaman 6Bryan Leaman 6
Oh, silly me. Looks like the issue was my <lightning:inputField> tag had "fieldname" instead of "fieldName" as the attribute. I didn't think attribute names were case sensitive.

All Answers

Raj VakatiRaj Vakati
You no need to pass the record Id for new record 

Do it like this
 
<aura:component>
    <lightning:recordEditForm
            onsubmit="{!c.newCreditAppSave}"
            objectApiName="dealer__Credit_Application__c">
            <lightning:messages />

                    <lightning:inputField fieldname="Finance_Company__c" aura:id="newca_financecompany"/>
                    <lightning:inputField fieldname="Proposal__c" aura:id="newca_proposal"/>
                    <lightning:button type="submit" label="Add Finance Company"/>
					
            <div class="slds-m-top_medium">
                <lightning:button disabled="{!v.disabled}" variant="brand" type="submit" name="save" label="Save" />
            </div>
        </lightning:recordEditForm>
       

</aura:component>

https://developer.salesforce.com/docs/component-library/bundle/lightning:recordEditForm/example#lightningcomponentdemo:exampleRecordCreate
Raj VakatiRaj Vakati
Use this code
 
<aura:component>
    <lightning:recordEditForm
            onsubmit="{!c.newCreditAppSave}"
            objectApiName="dealer__Credit_Application__c">
            <lightning:messages />

                    <lightning:inputField fieldname="Finance_Company__c" aura:id="newca_financecompany"/>
                    <lightning:inputField fieldname="Proposal__c" aura:id="newca_proposal"/>
                    <lightning:button type="submit" label="Add Finance Company"/>
					
            <div class="slds-m-top_medium">
                <lightning:button variant="brand" type="submit" name="save" label="Save" />
            </div>
        </lightning:recordEditForm>
       

</aura:component>

 
Maharajan CMaharajan C
Hi Bryan,

Try the Onload function in <lightning:recordEditForm> maybe that will Prepopulate your Id in the Lookupfield.

Like Below:

<lightning:recordEditForm aura:id="newcreditapp"
                    recordId="{!v.newCreditAppId}"
                    objectApiName="dealer__Credit_Application__c"
                    onsubmit="{!c.newCreditAppSave}"
                     onload="{!c.handleOnload}"
                    class="slds-card__body_inner">

In JS Controller :

handleOnload : function(component, event, helper) {
    // Here am using the Force:hasRecordId interface itself for setting the value if you want you can change. Because i hope you can get the parent ID from component.get("v.recordId") .
    component.find("newca_proposal").set("v.value", component.get("v.recordId")); 
    //
 component.find("newca_proposal").set("v.value", component.get("v.dealFields.Id"));
}

Can you please Let me know if it helps or not!!!

If it helps don't forget to mark this as a best answer!!!


Thanks,
Maharajan.C
Bryan Leaman 6Bryan Leaman 6
Thanks, Raj, I've tried it both ways and my input field still doesn't display. In it's place is a hidden <span> tag with the text "No input rendered: " "Field "undefined" not found in response."

Maharajan.C, The problem isn't that the value is not populating. The problem is my fields don't even render on the screen. If I don't try to fill in the lookup value I still don't get a field on the screen to manually select the value.
Bryan Leaman 6Bryan Leaman 6
Oh, silly me. Looks like the issue was my <lightning:inputField> tag had "fieldname" instead of "fieldName" as the attribute. I didn't think attribute names were case sensitive.
This was selected as the best answer