+ Start a Discussion
Jayesh BhatnagarJayesh Bhatnagar 

How to default a value to lightning:inputField lookup field?

I have a custom lightning component to create opportunities. This lightning component uses a lightning:recordEditForm and within it has a lightning:inputField lookup field. Everything is working fine. I am able to lookup an Account with the lightning:inputField lookup field and select a value and save the opportunity. My question is how do I default a value for the lightning:inputField lookup field? I am able to do this for the other text fields by specifying the value in my component. But there is no documentation for the lookup field defaulting. What is the format for this? Is it possible?
Raj VakatiRaj Vakati
<aura:component implements="flexipage:availableForAllPageTypes,force:appHostable" access="global"> 
<aura:attribute name="contacts" type="Contact" default="{'sobjectType':'Contact'}"/> 
<aura:handler name="init" value="{!this}" action="{!c.doInit}" /> 
<force:inputField aura:id="lookup" value="{!v.contacts.AccountId}"/> 
</aura:component>
 
({ 
doInit: function(component, event, helper){ 
var value = [{ 
type: 'Account', 
id: "001f100001HNc9iAAD", 
label: "navigate", 
}]; 
component.find("lookup").get("v.body")[0].set("v.values", value); 
}

Use the above code ... 
https://help.salesforce.com/articleView?id=000268561&language=en_US&type=1

 
Jayesh BhatnagarJayesh Bhatnagar
I found some examples of doing that with force:inputField but I need to use lightning:recordEditForm and nested lightning:inputField as documented in the lightning developers guide.
Jayesh BhatnagarJayesh Bhatnagar
I need to use lightning:recordEditForm and a nested lightning:inputField as suggested by the lightning components developer guide example for creating records with lookup values. My understanding is that force:inputField should no longer be used and does not follow SLDS?
Jayesh BhatnagarJayesh Bhatnagar
I tried the workaround of using force:inputField instead of lightning:inputField as suggested by the first responder above. But that did not work in my case either.

The help link referenced above states that the defaulting will not work for quick actions. In my case I am overriding the standard new opportunity button with my component using lightning:actionOverride. Does overriding a standard button also qualify as a quick action?

The force:inputField default does work when I add my component to the account lightning page. But my scenario is to override the new opportunity button. So still looking for answers. Any other suggestions?
Ashwin Kumar SrinivasanAshwin Kumar Srinivasan
Hi Jayesh,

You should be able to default the lookup field by setting the recordId in the value
<aura:component implements="force:appHostable" >
    <lightning:recordEditForm objectApiName="Contact">
        <lightning:messages />
        <lightning:inputField fieldName="FirstName" />
        <lightning:inputField fieldName="LastName" />
        <lightning:inputField fieldName="Email" />
        <lightning:inputField fieldName="AccountId" value="0011p00001ZQDGzAAP"/>
        <lightning:button class="slds-m-top_small" variant="brand" type="submit" name="update" label="Update" />
    </lightning:recordEditForm>
</aura:component>

Thanks
Ashwin Kumar SrinivasanAshwin Kumar Srinivasan
you might need to retrieve the id of the lookup from server side and not load recordEditForm until you get the value
or 
if you are using action from account to create opportunity with the account lookup, the value can be set from force:hasRecordId which will give you the recordId of the account.