You need to sign in to do that
Don't have an account?
loginaditya1.3936441421958025E12
How to pre-populate a lookup field in lightning:inputField?
I am trying to pre-populate lightning:inputField of type lookup on my custom lightning component. I tried passing Record Id OR Record Name as well as shown below. When the page is loaded I see a spinner on the field which keeps spinning and eventually the entire page (session) goes unresponsive.
I also tried populating value in "init" and in onLoad function of recordEditForm, to the same result mentioned above. Here is my sample code. Please note this is a sample code and not the exact code.
<aura:component implements="force:hasRecordId"> <lightning:recordEditForm objectApiName="CustomObject__c"> <lightning:messages /> <lightning:inputField fieldName="ContactLookup" aura:id="ContactLkp" value="0033D00000ONAN3"/> <lightning:button class="btn" type="submit" label="Save" /> </lightning:recordEditForm> </aura:component>
I also tried populating value in "init" and in onLoad function of recordEditForm, to the same result mentioned above. Here is my sample code. Please note this is a sample code and not the exact code.
<aura:component implements="force:hasRecordId"> <lightning:recordEditForm objectApiName="CustomObject__c"> <lightning:messages /> <lightning:inputField fieldName="ContactLookup" aura:id="ContactLkp" value="0033D00000ONAN3"/> <lightning:button class="btn" type="submit" label="Save" /> </lightning:recordEditForm> </aura:component>
Refer this link
https://help.salesforce.com/articleView?id=000268561&language=en_US&type=1
I have tried the below sample code for you it's working fine so please refer this with your component.
1. Set the value from the OnLoad in Record Edit Form but it will take some time to load the Lookupdata the spinner will come :
Component:
<aura:component implements="flexipage:availableForAllPageTypes" access="global">
<aura:attribute name="recordId" type="String"/>
<aura:attribute name="parentId" type="String"/>
<lightning:notificationsLibrary aura:id="notificationsLibrary"/>
<lightning:card iconName="standard:account"
title="{! (v.recordId == null ? 'New ' : 'Edit ') + 'Contact' }">
<lightning:recordEditForm aura:id="form"
recordId="{!v.recordId}"
objectApiName="Contact"
onload="{!c.handleOnload}"
onsuccess="{!c.handleOnSuccess}"
class="slds-card__body_inner">
<lightning:messages/>
<lightning:inputField fieldName="Name"/>
<lightning:inputField fieldName="AccountId" aura:id="accountLookup"/>
<lightning:inputField fieldName="Email" aura:id="EmailId"/>
<div class="slds-m-top_medium">
<lightning:button variant="brand" label="Submit" type="submit" />
</div>
</lightning:recordEditForm>
</lightning:card>
</aura:component>
Controller:
({
handleOnload : function(component, event, helper) {
component.set("v.parentId","0017F00000q7Jo6QAE");
var parentId = component.get("v.parentId");
component.find("accountLookup").set("v.value", parentId);
component.find("EmailId").set("v.value", "maharajansfdc@gmail.com");
},
handleOnSuccess : function(component, event, helper) {
var record = event.getParam("response");
component.find("notificationsLibrary").showToast({
"title": "Saved",
"message": "{0} saved for {1}",
"messageData": [
{
url: '/' + record.id,
label: record.fields.FirstName.value + ' ' + record.fields.LastName.value
},
{
url: '/' + record.fields.AccountId.value,
label: record.fields.Account.displayValue
}
]
});
}
})
2. Tried the Default Value in the Attribute as Like below It don't take take that much time to load the Lookupdata No Spinner Comes:
<aura:attribute name="parentId" type="String" default="0017F00000q7Jo6QAE"/>
Controller:
handleOnload : function(component, event, helper) {
var parentId = component.get("v.parentId");
component.find("accountLookup").set("v.value", parentId);
component.find("EmailId").set("v.value", "maharajansfdc@gmail.com");
}
3. Use the Init to load the Lookupdata tt also don't take take that much time No Spinner Comes:
<aura:attribute name="parentId" type="String"/>
<aura:handler name="init" value="{!this}" action="{!c.doInit}" />
Controller:
doInit : function(component, event, helper){
component.set("v.parentId","0017F00001JN2OMQA1");
},
handleOnload : function(component, event, helper) {
var parentId = component.get("v.parentId");
// requires inputFields to have aura:id
component.find("accountLookup").set("v.value", parentId);
component.find("EmailId").set("v.value", "maharajansfdc@gmail.com");
},
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
So long story short, the problem was not with the way I was populating the Ids in JS controller, but the Id itself which was being used. It should be 18 digit Id.
(Just for information, Apex controller returns 18 digit Id whereas formula editor gives you 15 digit Id.)