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
Valli KValli K 

prepopulate lookup field in lightning:recordeditform with lightning:inputfield

prepopulate lookup field in lightning:recordeditform with lightning:inputfield
Khan AnasKhan Anas (Salesforce Developers) 

Hi Valli,

Greetings to you!

You can use the below code:
 
<aura:component implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,forceCommunity:availableForAllPageTypes,force:lightningQuickAction" access="global" >
    
    <div class="slds-m-around_xx-large">
        <lightning:recordEditForm recordId="5007F00000SbdGaQAJ" objectApiName="Case">
            <lightning:messages />
            <lightning:inputField fieldName="AccountId"/>
            <lightning:inputField fieldName="ContactId"/>
            <lightning:inputField fieldName="Origin" />
            <lightning:inputField fieldName="Status" />
            <lightning:inputField fieldName="Priority" />
            <lightning:button class="slds-m-top_small" variant="brand" type="submit" name="update" label="Update" />
        </lightning:recordEditForm>
    </div>
</aura:component>

Replace recordId with your Case Id in above example.

Please refer to the below link with a similar discussion which might help you further.

https://salesforce.stackexchange.com/questions/215218/lightningrecordeditform-not-populating-lookup-fields

https://salesforce.stackexchange.com/questions/219142/how-can-i-pre-populate-a-lookup-field-in-lightninginputfield

https://developer.salesforce.com/forums/?id=9060G0000005ZhkQAE

http://peterknolle.com/lightning-record-edit-form/


I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future.

Thanks and Regards,
Khan Anas
Valli KValli K
<aura:component controller="controller" implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,forceCommunity:availableForAllPageTypes,force:lightningQuickAction" access="global" >
     <aura:handler name="init" value="{!this}" action="{!c.doInit}" />
    <aura:attribute name="sid" type="String"/>
     <aura:attribute name="recordId" type="String"/>
    <aura:attribute name="parentId" type="String"/>
 
     <lightning:recordEditForm aura:id="divisionCreateForm" recordId="{!v.recordId}" objectApiName="customobject_c"
                                  onsubmit="{!c.handleSubmit}" onload="{!c.handleOnload}">
            
                       <div class="slds-size_1-of-2">
                                <div class="slds-m-around_x-small"> 
                                    <lightning:inputField aura:id="lookup" fieldName="Account__c"/>
                                </div>
                            </div>                   
              
          <div class="slds-align_absolute-center slds-m-top_medium"> 
                  <lightning:button  variant="brand" type="submit" name="save" label="Save" / >
         </div>
           </lightning:recordEditForm>
              </aura:component>
=====================
({
    doInit : function(component, event, helper) {
        
    },
    handleOnload : function(component, event, helper) {
    var parentId = component.get("v.parentId");

    // requires inputFields to have aura:id
    component.find("lookup").set("v.value", parentId);
   
},
    handleSubmit : function(component,event,helper){
        helper.createDivision(component,event);
    },
    
})============
({
    createDivision: function(component, event) {
        var s = component.get("v.sid");
        var enteredFields = event.getParam("fields");
        console.log("enteredFields JSON:: "+JSON.stringify(enteredFields));
        var JsonString = JSON.stringify(enteredFields);
        var createAction=component.get("c.createDivision");
        createAction.setParams({
            "newdivisionJSON":JsonString
        });
        createAction.setCallback(this,function(response){
                var state=response.getState();
            if(state=='SUCCESS'){
               console.log('success');
              s = response.getReturnValue();
                console.log(s);
            component.set("v.sid",s);
        var evt = $A.get("e.force:navigateToSObject");
        console.log("navEvt==="+evt);
        evt.setParams({
            "recordId": s
        });
        evt.fire(); 
                console.log('event fired');
            }
            else{
                console.log('error');
            }
        });
        $A.enqueueAction(createAction);  
    },
   
})
Valli KValli K
@Khan Anas   - Posted is my code Accountid is not populating... only lookup fields need to prepopulate it.. but not for other  fields. Could you corect my code
 
Raj VakatiRaj Vakati

When using lightning:inputField for the fields if type ‘reference’ (look up), lookup search does not load the modal for the first time. It works fine after we reload the page

If know issue  .. 



https://success.salesforce.com/issues_view?id=a1p3A000000mDV2QAM
https://success.salesforce.com/ideaView?id=0873A000000E9xzQAC


Refer this code . *"0037F00000XXXXXXXX" should be recordId you need

 
<aura:component implements="force:appHostable" > 
<lightning:recordEditForm aura:id="recordEditForm" recordId="0037F00000XXXXXXXX" objectApiName="Contact"> 
<lightning:inputField fieldName="AccountId"/> 
</lightning:recordEditForm> 
</aura:component>

 
Mrinal Sharma 23Mrinal Sharma 23
@Valli
Did you get your answer? 
I am creating new record using <lightning:recordEditForm>  and i am not able to populate lookup field onload finction. 
I tried below things but both are not working: 
1.
<lightning:inputField aura:id="caseLookup" type='String'  fieldName ="Case__c" value="00842156"/>
2.
<lightning:inputField aura:id="caseLookup" fieldName ="Case__c"/>
Controller: 
onload : function(component, event, helper) {
        var parentId = component.get("v.parentId");
        // requires inputFields to have aura:id
        component.find("caseLookup").set("v.value", "00842156");
    },
 
Please help me here, I can do it using custom component but i want to use this.
 
pradempradem
@Mrial Sharma
replace "00842156" with the case Id instead of case number
replace  component.find("caseLookup").set("v.value", "00842156"); with component.find("caseLookup").set("v.value", "<CaseId>");