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
Firas Taamallah 12Firas Taamallah 12 

How to save multi records on lookup field using LC ?

Lightning component : 
 
<aura:component implements="flexipage:availableForRecordHome,force:hasRecordId" controller="SaveFormVisitReport" >
<aura:attribute name="recordId" type="String" />
<aura:attribute name="selectedLookUpRecords" type="Contact" default="[]"/>
<aura:attribute name="selectedLookUpRecords1" type="User" default="[]"/>
<aura:attribute name="newRecord" type="Visit_Report__c" default="{ 'sobjectType': 'Visit_Report__c',                                                                      'Visit_Type__c':'',                                                                        'Contact__c':'',                                                                        'User__c':'',                                                                       'Account__c':'',                                                                       'Comments__c':''}"/>
 <lightning:recordEditForm aura:id="myform" objectApiName="Visit_Report__c" onsubmit="{!c.handleSubmit}">
<lightning:messages />
<lightning:inputField fieldName="Visit_Type__c" aura:id="type"/>   
<lightning:inputField fieldName="User__c" aura:id="user"/> 
<c:reUsableMultiSelectLookup objectAPIName="Contact"
                               IconName="standard:contact"
                               lstSelectedRecords="{!v.selectedLookUpRecords}"
                               label="Contact Name" aura:id="cont"/>
<c:reUsableMultiSelectLookup objectAPIName="User"
                               IconName="standard:User"
                               lstSelectedRecords="{!v.selectedLookUpRecords1}"
                               label="Assigned to" aura:id="user"/> 
<lightning:inputField fieldName="Account__c" aura:id="acc"/> 
<lightning:inputField fieldName="Comments__c" aura:id="comm"/>
<lightning:button
                class="slds-m-top_small"
                type="submit"
                label="Create new">
</lightning:button>
</lightning:recordEditForm>
</aura:component>

Controller JS :
 
handleSubmit: function(component, event, handler) {
      var input1 = component.get("v.selectedLookUpRecords");
      var input2 = component.get("v.selectedLookUpRecords1");
      component.find('cont').set('v.value', input1);
      component.find('acc').set('v.value', input2);
      component.find('myform').submit();
    }

Contact and user haven't been saved - Please note c:reUsableMultiSelectLookup is another lightning component , it's goal : user be able to select multi records on lookup field

Form
AbhishekAbhishek (Salesforce Developers) 
Hi Firas,

Your query is answered in this blog,

https://salesforce.stackexchange.com/questions/302998/save-form-lightning-component

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks.
Firas Taamallah 12Firas Taamallah 12
Hi @Abhishek  ,  that's my question on StackExchange 
Unfortunately , it's not solved yet ..
David Zhu 🔥David Zhu 🔥
If using component.find('myform').submit(); only the fields of the object which ligthing:recordEditForm> will be saved. In your case, the fields on Visit_Report__c object.

To handle saving records on multiple objects, you need to add an apex method and call the method from js controller/helper.
 
Firas Taamallah 12Firas Taamallah 12
David  , do you mean i need to use soql to retrive contact and user ?